- Start Date: 2014-05-21
- RFC PR: rust-lang/rfcs#85
- Rust Issue: rust-lang/rust#14473
Summary
Allow macro expansion in patterns, i.e.
match x {
my_macro!() => 1,
_ => 2,
}
Motivation
This is consistent with allowing macros in expressions etc. It’s also a year-old open issue.
I have implemented this feature already and I’m using it to condense some ubiquitous patterns in the HTML parser I’m writing. This makes the code more concise and easier to cross-reference with the spec.
Drawbacks / alternatives
A macro invocation in this position:
match x {
my_macro!()
could potentially expand to any of three different syntactic elements:
- A pattern, i.e.
Foo(x)
- The left side of a
match
arm, i.e.Foo(x) | Bar(x) if x > 5
- An entire
match
arm, i.e.Foo(x) | Bar(x) if x > 5 => 1
This RFC proposes only the first of these, but the others would be more useful in some cases. Supporting multiple of the above would be significantly more complex.
Another alternative is to use a macro for the entire match
expression, e.g.
my_match!(x {
my_new_syntax => 1,
_ => 2,
})
This doesn’t involve any language changes, but requires writing a complicated procedural macro. (My sustained attempts to do things like this with MBE macros have all failed.) Perhaps I could alleviate some of the pain with a library for writing match
-like macros, or better use of the existing parser in libsyntax
.
The my_match!
approach is also not very composable.
Another small drawback: rustdoc
can’t document the name of a function argument which is produced by a pattern macro.
Unresolved questions
None, as far as I know.