Rematch2
Documentation for Rematch2.
Rematch2.@ismatchRematch2.@matchRematch2.@matchRematch2.@matchRematch2.@match2Rematch2.@match2Rematch2.@match_failRematch2.@match_return
Rematch2.@ismatch — MacroUsage:
@ismatch value patternReturn true if value matches pattern, false otherwise. When returning true, binds the pattern variables in the enclosing scope. Typically, it would be used like this:
if @ismatch point Point(0, y)
println("On the y axis at y = $y")
endor
if (@ismatch point Point(x, y)) && x < y
println("The point ($x, $y) is in the upper left")
endguarded patterns ought not be used with @ismatch, as you can just use && instead.
Rematch2.@match — MacroThis macro has two forms.
- The single-pattern form:
@match2 pattern = valueIf value matches pattern, bind variables and return value. Otherwise, throw MatchFailure.
- The multi-pattern form:
@match2 value begin
pattern1 => result1
pattern2 => result2
...
endReturn result for the first matching pattern. If there are no matches, throw MatchFailure.
Patterns:
_matches anythingfoomatches anything, binds value tofooFoo(x,y,z)matches structs of typeFoowith fields matchingx,y,zFoo(y=1)matches structs of typeFoowhoseyfield equals1[x,y,z]matchesAbstractArrays with 3 entries matchingx,y,z(x,y,z)matchesTuples with 3 entries matchingx,y,z[x,y...,z]matchesAbstractArrays with at least 2 entries, wherexmatches the first entry,zmatches the last entry andymatches the remaining entries.(x,y...,z)matchesTuples with at least 2 entries, wherexmatches the first entry,zmatches the last entry andymatches the remaining entries._::Tmatches any subtype (isa) of Tx || ymatches values which match eitherxory(only variables which exist in both branches will be bound)x && ymatches values which match bothxandyx where conditionmatches only ifconditionis true (conditionmay use any variables that occur earlier in the pattern eg(x, y, z where x + y > z))- Anything else is treated as a constant and tested for equality
- Expressions can be interpolated in as constants via standard interpolation syntax
$(x)
Patterns can be nested arbitrarily.
Repeated variables only match if they are equal (==). For example (x,x) matches (1,1) but not (1,2).
Rematch2.@match — MacroUsage:
@match value begin
pattern1 => result1
pattern2 => result2
...
endReturn result for the first matching pattern. If there are no matches, throw MatchFailure. This uses a brute-force code gen strategy, like using a series of if-else statements. It is used for testing purposes, as a reference for correct semantics.
Rematch2.@match — MacroUsage:
@match pattern = valueIf value matches pattern, bind variables and return value. Otherwise, throw MatchFailure.
Rematch2.@match2 — MacroUsage:
@match2 value begin
pattern1 => result1
pattern2 => result2
...
endReturn result for the first matching pattern. If there are no matches, throw MatchFailure. This is like @match, but generaties more efficient code.
Rematch2.@match2 — MacroUsage:
@match2 pattern = valueIf value matches pattern, bind variables and return value. Otherwise, throw MatchFailure.
Rematch2.@match_fail — Macro@match_failThis statement permits early-exit from the value of a @match2 case. The programmer may write the value as a begin ... end and then, within the value, the programmer may write
@match_failto cause the case to terminate as if its pattern had failed. This permits cases to perform some computation before deciding if the rule "really" matched.
Rematch2.@match_return — Macro@match_return valueThis statement permits early-exit from the value of a @match2 case. The programmer may write the value as a begin ... end and then, within the value, the programmer may write
@match_return valueto terminate the value expression early with success, with the given value.