Pattern Synonyms?


#1

Has someone suggested the idea of adding Pattern Synonyms in Rust?
https://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms

Given a type like:

struct Type<'a>(&'static str, &'a [Type<'a>]);

You can think a Rust-like syntax like:

pattern Int = Type(“Int”, &[]);
pattern Arrow(t1, t2) = Type("->", &[t1, t2]);
pattern Foo = 1 | 2 | 7;

Usable as shorthand for patterns in match{}.

How much often do you wish to use such synonyms?

This idea is related but different from Active Patterns of F# language:


#2

Well…

macro_rules! Int { () => { Type("Int", &[]) } }
macro_rules! Arrow { ($t1:pat, $t2:pat) => { Type("->", &[$t1, $t2]) } }

But you can’t (IIRC) do Foo because | isn’t part of the pattern; it’s part of the match syntax (like guard conditions).