You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Having a way of matching against a set of destructuring patterns would make the language quite a bit more expressive.
Patterns
The following pattern types should be supported:
_ - wildcard pattern. _ becomes a keyword and is used for discarding values in patterns.
nil, true, 1, "abc" - literal patterns. For now only constant literals shall be supported
abc - variable binding pattern. The variable name can be prefixed with mut to make the variable mutable (see Deep immutability #68)
1 or 2 - or-pattern, matches the left-hand side or the right-hand side
Additional patterns can be added as the language's feature set is expanded with tuples, lists, and dicts.
I also think constraints should be moved over to patterns, as that will allow for easier error checking in a lot of cases.
Assignment
Assignment shall use a pattern instead of an identifier on the left-hand side. If matching fails, a runtime error is produced.
1 = 1 # ok
a = 1 # ok
2 = nil # error: pattern doesn't match
2 + 1 = 3 # error: invalid pattern - only literals, variables, and or is supported
Failing assignment does not always have to terminate execution, though. To allow for greater expressiveness, in the future we could make a special case for if and while where if the expression is an assignment, failure is considered falsy.
Because assignment is now a "match pattern or fail" operator, having it return a value wouldn't make much sense, so that functionality shall be dropped and nil should be produced instead of the old value stored in a variable.
match expression
A match expression should be introduced as a control flow construct to accompany if. match is comprised of a list of arms, with each arm being pattern -> expression. Arms are newline-separated.
x match
p1 -> e1
p2 -> e2
p3 -> do
x = 1 # do stuff
e3
end
p4 -> e4
end
We use an infix match to better enable chaining multiple matches together.
The text was updated successfully, but these errors were encountered:
Having a way of matching against a set of destructuring patterns would make the language quite a bit more expressive.
Patterns
The following pattern types should be supported:
_
- wildcard pattern._
becomes a keyword and is used for discarding values in patterns.nil
,true
,1
,"abc"
- literal patterns. For now only constant literals shall be supportedabc
- variable binding pattern. The variable name can be prefixed withmut
to make the variable mutable (see Deep immutability #68)1 or 2
- or-pattern, matches the left-hand side or the right-hand sideAdditional patterns can be added as the language's feature set is expanded with tuples, lists, and dicts.
I also think constraints should be moved over to patterns, as that will allow for easier error checking in a lot of cases.
Assignment
Assignment shall use a pattern instead of an identifier on the left-hand side. If matching fails, a runtime error is produced.
Failing assignment does not always have to terminate execution, though. To allow for greater expressiveness, in the future we could make a special case for
if
andwhile
where if the expression is an assignment, failure is considered falsy.Because assignment is now a "match pattern or fail" operator, having it return a value wouldn't make much sense, so that functionality shall be dropped and
nil
should be produced instead of the old value stored in a variable.match
expressionA
match
expression should be introduced as a control flow construct to accompanyif
.match
is comprised of a list of arms, with each arm beingpattern -> expression
. Arms are newline-separated.We use an infix
match
to better enable chaining multiple matches together.The text was updated successfully, but these errors were encountered: