Skip to content
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

Closed
liigo opened this issue Dec 24, 2013 · 4 comments
Closed

auto-unwrap: make foo()! the same as foo().unwrap() #11133

liigo opened this issue Dec 24, 2013 · 4 comments

Comments

@liigo
Copy link
Contributor

liigo commented Dec 24, 2013

Someone want to remove one of .foo() and .foo_opt(), but that will let us write more foo().unwrap(), which is a bit nasty. I'd like to see introducing a new syntax sugar: foo()!, which means the same as foo().unwrap(). The ! here means: 1) ensure it's not None, 2) it maybe fail!.

We can define a trait Unwrapable<T> that has an fn unwrap() -> T, and impl it for Option, Result<T,E> and others. When ! operated on an Unwrapable value, rustc call its unwrap() 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().

@jfager
Copy link
Contributor

jfager commented Dec 24, 2013

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 let a = foo.unwrap_or_else(|| fail()!) every time.

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.

@liigo
Copy link
Contributor Author

liigo commented Dec 25, 2013

@jfager There are many .unwrap() in rust source repository. Are they all misusage? I don't think so. This is a common usage, authors need it, they ensure got Some(T) not None in that situation, and even fail! is acceptable. Are you like code full of let a = foo.unwrap_or_else(|| fail()!) everywhere? match need at least 4 lines.

@aturon
Copy link
Member

aturon commented Jun 3, 2014

cc me

@pnkfelix
Copy link
Member

pnkfelix commented Jun 3, 2014

Closing: A change like this needs to go through the RFC process that we imposed earlier this year: https://github.com/rust-lang/rfcs/

@pnkfelix pnkfelix closed this as completed Jun 3, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 14, 2023
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants