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

A macro fragment matcher analogous to right-hand side of a match arm #773

Open
mzabaluev opened this issue Jan 30, 2015 · 0 comments
Open
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@mzabaluev
Copy link
Contributor

I have some code for dynamically typed error handling, and to provide syntactic sugar over nested match expressions I have a macro with syntax that itself mimics a single match expression:

error_match! {
    (err) {
        (a_err: DomainError<A>) => {
            // ...
        }  // note no comma here
        (b_err: DomainError<B>) => (),  // an expression followed by a comma
        // ...
    }
}

There does not seem to be a way to alternate different matchers in repetition sequences, and furthermore, the current rules disallow block fragments followed by repetition sequences. So in realizing such macro recursively I'm limited to comma-appended expressions at the end of each arm, like so:

(
    ($inp:expr) {
        ($slot:ident : $errtype:ty) => $handler:expr,
        $(($slot_tail:ident : $errtype_tail:ty) => $handler_tail:expr),*
    }
) => { ... }

The need to put a comma after a block is annoying, as the real match statement syntax forbids commas in this position. So I'd like there to be a fragment specifier that would make the fragment match either an expression followed by a comma, or a block. The macro future-proofing rules for fragments of this type would allow them to immediately precede sequence repetition and end a repeating sequence, so the invocation pattern above could be extended as follows:

(
    ($inp:expr) {
        ($slot:ident : $errtype:ty) => $handler:match_handler
        $(($slot_tail:ident : $errtype_tail:ty) => $handler_tail:match_handler)*
    }
) => { ... }
@petrochenkov petrochenkov added the T-lang Relevant to the language team, which will review and decide on the RFC. label Jan 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

2 participants