Rematch2
Documentation for Rematch2.
Rematch2.@ismatch
Rematch2.@match
Rematch2.@match
Rematch2.@match
Rematch2.@match2
Rematch2.@match2
Rematch2.@match_fail
Rematch2.@match_return
Rematch2.@ismatch
— MacroUsage:
@ismatch value pattern
Return 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")
end
or
if (@ismatch point Point(x, y)) && x < y
println("The point ($x, $y) is in the upper left")
end
guarded 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 = value
If value
matches pattern
, bind variables and return value
. Otherwise, throw MatchFailure
.
- The multi-pattern form:
@match2 value begin
pattern1 => result1
pattern2 => result2
...
end
Return result
for the first matching pattern
. If there are no matches, throw MatchFailure
.
Patterns:
_
matches anythingfoo
matches anything, binds value tofoo
Foo(x,y,z)
matches structs of typeFoo
with fields matchingx,y,z
Foo(y=1)
matches structs of typeFoo
whosey
field equals1
[x,y,z]
matchesAbstractArray
s with 3 entries matchingx,y,z
(x,y,z)
matchesTuple
s with 3 entries matchingx,y,z
[x,y...,z]
matchesAbstractArray
s with at least 2 entries, wherex
matches the first entry,z
matches the last entry andy
matches the remaining entries.(x,y...,z)
matchesTuple
s with at least 2 entries, wherex
matches the first entry,z
matches the last entry andy
matches the remaining entries._::T
matches any subtype (isa
) of Tx || y
matches values which match eitherx
ory
(only variables which exist in both branches will be bound)x && y
matches values which match bothx
andy
x where condition
matches only ifcondition
is true (condition
may 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
...
end
Return 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 = value
If value
matches pattern
, bind variables and return value
. Otherwise, throw MatchFailure
.
Rematch2.@match2
— MacroUsage:
@match2 value begin
pattern1 => result1
pattern2 => result2
...
end
Return 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 = value
If value
matches pattern
, bind variables and return value
. Otherwise, throw MatchFailure
.
Rematch2.@match_fail
— Macro@match_fail
This 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_fail
to 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 value
This 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 value
to terminate the value expression early with success, with the given value.