Skip to content

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

@mzabaluev

Description

@mzabaluev

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)*
    }
) => { ... }

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions