-
Notifications
You must be signed in to change notification settings - Fork 39
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
Extended pattern matching #182
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a simple extension to the current pattern matching facility, I’d like to allow
match
on literals, since they’re essentially the “constructors” of intrinsic types:For better usability, pattern matching needs to be extended to support more than just constructors. I have a rough idea of what it ought to look like, but haven’t worked out all the semantics.
Matching on
List
would be a good first step. Examples:case []
: empty listcase [...]
: any listcase [_]
: any list of length 1case [_, _, _]
: any list of length 3case [a, _]
: any list of length 1, bindinga
case [a, ...]
: any list of length 1 or greater, binding the first elementcase [..., a]
: any list of length 1 or greater, binding the last elementcase [0, ..., 1]
: any list of length 2 or greater, beginning with 0 and ending with 1case [1, ..., ...]
: error, adjacent...
case [1, ..., 2, ..., 3]
: error, patterns between...
case [_, ...]
: any list of length 1 or greatercase "abc"
: sugar forcase ['a', 'b', 'c']
For other constructors, I think it makes sense to require parentheses and write them in postfix like constructor application. The last word in a parenthesised pattern is the constructor.
As for lists,
...
could be used to ignore multiple adjacent fields.The
as
keyword could be reused to bind the result of a pattern match to a name, like Haskell’s@
(“as-patterns”) or OCaml’sas
.Guards could reuse the
if
keyword; acase
could accept anif
/elif
/else
expression as a body, and fall through to the next pattern if no condition matched. Unfortunately this is ambiguous withelse
as used inmatch
, so maybe it needs to be written ascase else
or something.Exhaustiveness checking also becomes much more involved with more complex patterns.
The text was updated successfully, but these errors were encountered: