Skip to content

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

Closed
@liigo

Description

@liigo

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions