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

Consider a macro corresponding to Rust's ? operator #6552

Open
daira opened this issue Apr 14, 2023 · 1 comment · May be fixed by #6554
Open

Consider a macro corresponding to Rust's ? operator #6552

daira opened this issue Apr 14, 2023 · 1 comment · May be fixed by #6554
Assignees
Labels

Comments

@daira
Copy link
Contributor

daira commented Apr 14, 2023

Example based on https://github.com/zcash/zcash/pull/6524/files#r1166886694 :

    auto maybeChangeAddr = /* a tl::expected<ChangeAddress, AddressResolutionError> */;
    if (maybeChangeAddr.has_value()) {
        changeAddr = maybeChangeAddr.value();
    } else {
        return tl::make_unexpected(maybeChangeAddr.error());
    }

It should be possible to write this as something like:

    changeAddr = unwrap { /* a tl::expected<ChangeAddress, AddressResolutionError> */ } or_return;

where the syntax using {} rather than parens is intended to call out that this can return and is not just a function call (while still being implementable).

See also:

@daira
Copy link
Contributor Author

daira commented Apr 23, 2023

I ended up with a slightly different syntax that is a bit more robust to parsing problems (i.e. it gives less inexplicable errors if you get the syntax incorrect). The initial macro name needs to be try_unwrap because unwrap is also used in Boost headers, and that caused problems with the macro being expanded unintentionally. I also added some other variants as documented in #6554.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant