I would like to nominate `Option::as_deref` for the worst named method in Rust
-
-
Replying to @sgrif
what would you call it? ... i remember being kind of frustrated by how long it was unstable because of that particular bikeshed, and it’s really way nicer than .map(|x| &*x)
2 replies 0 retweets 0 likes -
Replying to @at_tcsc
First preference: `Deref` was just structured in a way it could be implemented by `Option<impl Deref>`. Second preference: Inherent method called `deref` Third+ preference: Something that implies it's *calling* deref on its member, e.g. `derefed`.
2 replies 0 retweets 0 likes -
Replying to @sgrif
how would the first preference work? how do you handle None?
2 replies 0 retweets 0 likes -
Replying to @mountain_ghosts
It'd return `None`. This would require GATs so that it was `fn deref(&self) -> Self::Target<'_>`, letting you return an option of a reference for example.
1 reply 0 retweets 0 likes -
Replying to @sgrif
oh I'd not thought of it like that: of course Option<T> could impl Deref with Target = Option<<T as Deref>::Target>, which wouldn't make Option<T> transparently equal to T under Deref, but I'd not considered the lifetime problem that requires GAT
1 reply 0 retweets 0 likes -
Replying to @mountain_ghosts
Ironically you can almost implement this for `Option`, since null ptr optimization means that `&T == &Option<T>` in terms of layout. Except it's not particularly useful since that'd require `T: Sized`, meaning `Option<String>` couldn't impl `Deref`
2 replies 0 retweets 0 likes -
-
And you'd want `impl Deref for Option<String>` to return an `Option<&str>`. The signature today would require returning an `&Option<str>` instead though, which isn't valid
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.