3 ms·
> String and &str can normally be compared because of Deref, that is, &String derefs to &str. Option, on the other hand, does not implement Deref, and so no coe
by JoshTriplett 10y ago
> String and &str can normally be compared because of Deref, that is, &String derefs to &str. Option, on the other hand, does not implement Deref, and so no coercion happens.
For this particular case, any particular reason we couldn't add an impl of PartialEq? In fact, once we have specialization, couldn't we have a general impl of PartialEq for Options of Deref types?
- steveklabnik 10y agoimpl<T> PartialEq<Option<T>> for Option<T> where T: PartialEq<T> does exist, but I'm not 100% sure why Deref doesn't kick in here, I just know that it doesn't. > couldn't we have a general impl of PartialEq for Options of Deref types? I think the issue is None. You'd get a null pointer, which doesn't make any sense in safe rust.
- JoshTriplett 10y ago> I think the issue is None. You'd get a null pointer, which doesn't make any sense in safe rust. No, I don't mean an impl between Option<T> and T, I mean a bidirectional impl of PartialEq between Option<T> and Option<U> where U is T's Deref::Target. In that case, None doesn't cause an issue; None == None, and Some(t) == Some(u) iff *t == u
- steveklabnik 10y agoAh right, yes.