These are chat archives for rust-lang/rust

25th
Apr 2016
Pete Hayes
@petehayes102
Apr 25 2016 09:04
Hey guys. I’m getting a pretty popular error when passing None to a fn with the signature fn …(var: Option<Trait>):
error: unable to infer enough type information about `_`
How does one get around this?
Apart from being a competent programmer :)
Erik Hedvall
@Ogeon
Apr 25 2016 09:06
You could always use Option::<Whatever>::None (or if it was Option<Whatever>::None)
Daniel Collin
@emoon
Apr 25 2016 09:08
I have never needed to do that but maybe I haven't used traits enough :)
Pete Hayes
@petehayes102
Apr 25 2016 09:08
True, but this is a public function, so I don’t want users to have to explicitly specify a type like that. Not very friendly
I’m binding to a C fn that takes a void ptr, so the value could be literally anything
Daniel Collin
@emoon
Apr 25 2016 09:09
Can you do a small repro over at https://play.rust-lang.org/ ?
Pete Hayes
@petehayes102
Apr 25 2016 09:09
Rust doesn’t like that!
Sure. Give me a few minutes.
Erik Hedvall
@Ogeon
Apr 25 2016 09:12
The inner type has to be specified, one way or the other. void is more analogous to Rust's Any, while None is more or less null
@Ogeon Ah! I didn’t know about the Any trait.
Erik Hedvall
@Ogeon
Apr 25 2016 09:29
A simple solution is to have a separate function for the null case: http://is.gd/L9J36U
It may have some slight discoverability issues, though, but the naming should suggest that there's a version without the parameter
Pete Hayes
@petehayes102
Apr 25 2016 09:39
That seems like the best compromise. I’m still getting the same error when implementing the Any trait.
Erik Hedvall
@Ogeon
Apr 25 2016 09:42
Yeah, it needs a concrete type. You could also take Option<Box<Any>>, and let the user take care of the boxing
*a known concrete type, that is
Pete Hayes
@petehayes102
Apr 25 2016 09:49
Yeah that’s an even better idea. Thanks mate!