-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auto-unwrap: make foo()!
the same as foo().unwrap()
#11133
Comments
Isn't the reason having 'unwrap' everywhere is a smell is because it ignores the safety net Option gives you and risks task failure everywhere it's used? This does the exact same thing but seems to bless it with special syntax. At least when you have to type out the full 'unwrap()' method you're having to work a little bit for it, and it's clear from it's ugliness that it should maybe be avoided. From an affordance standpoint, it seems unfortunate that the 'wrong' thing (just fail) is easier to reach for than the 'right' thing (generate a default or otherwise handle the error), so maybe it should even be a little uglier, and all Option methods that can fail should be removed, forcing you to write out If there's going to be special handling for Option or other Unwrappables, it should take the form of something like do-notation or Option chaining. |
@jfager There are many |
cc me |
Closing: A change like this needs to go through the RFC process that we imposed earlier this year: https://github.com/rust-lang/rfcs/ |
…r=Manishearth Fix typo in `needless_pass_by_ref_mut` lint description Someone nicely showed me that I made a small typo in rust-lang/rust-clippy#10900. changelog: none
Someone want to remove one of
.foo()
and.foo_opt()
, but that will let us write morefoo().unwrap()
, which is a bit nasty. I'd like to see introducing a new syntax sugar:foo()!
, which means the same asfoo().unwrap()
. The!
here means: 1) ensure it's not None, 2) it maybe fail!.We can define a trait
Unwrapable<T>
that has an fnunwrap() -> T
, and impl it for Option, Result<T,E> and others. When!
operated on an Unwrapable value, rustc call itsunwrap()
automatically. (I do not know whether or not it will be ambiguous with macro!.)After it is accepted, we may safely remove one of .foo() and .foo_opt().
The text was updated successfully, but these errors were encountered: