This will evaluate expression some_value and compare the result against value1, value2, ... sequentially. If anything "matches" (is equal to some_value), the corresponding result is returned. Otherwise, whatever is in the optional else block is returned.
The results and values can be any expression including blocks. values are evaluated lazily; if a match is found, no values evaluate further.
match arms must currently end in a comma including the final arm. Rationale: it's the rustfmt default behavior, and this specific match syntax is inspired by Rust. Enforcing this rule also helps with parsing a little since no extra peeks are required. This can be changed if needed.
match returns undefined if the arms block is empty or if no conditions were met without an else block present.
The => arrow token is now a thing too. It's only used for match as of now.
The current match-{1..5} unit tests don't cover everything, but that is totally fixable. Need a thorough review of what is done so far before adding more code.
The syntax is as follows:
This will evaluate expression
some_value
and compare the result againstvalue1
,value2
, ... sequentially. If anything "matches" (is equal tosome_value
), the correspondingresult
is returned. Otherwise, whatever is in the optionalelse
block is returned.The
result
s andvalue
s can be any expression including blocks.value
s are evaluated lazily; if a match is found, novalue
s evaluate further.match
arms must currently end in a comma including the final arm. Rationale: it's therustfmt
default behavior, and this specificmatch
syntax is inspired by Rust. Enforcing this rule also helps with parsing a little since no extrapeek
s are required. This can be changed if needed.match
returnsundefined
if the arms block is empty or if no conditions were met without anelse
block present.The
=>
arrow token is now a thing too. It's only used formatch
as of now.The current
match-{1..5}
unit tests don't cover everything, but that is totally fixable. Need a thorough review of what is done so far before adding more code.