Rematch2

Documentation for Rematch2.

Rematch2.@ismatchMacro

Usage:

    @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.

source
Rematch2.@matchMacro

This 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 anything
  • foo matches anything, binds value to foo
  • Foo(x,y,z) matches structs of type Foo with fields matching x,y,z
  • Foo(y=1) matches structs of type Foo whose y field equals 1
  • [x,y,z] matches AbstractArrays with 3 entries matching x,y,z
  • (x,y,z) matches Tuples with 3 entries matching x,y,z
  • [x,y...,z] matches AbstractArrays with at least 2 entries, where x matches the first entry, z matches the last entry and y matches the remaining entries.
  • (x,y...,z) matches Tuples with at least 2 entries, where x matches the first entry, z matches the last entry and y matches the remaining entries.
  • _::T matches any subtype (isa) of T
  • x || y matches values which match either x or y (only variables which exist in both branches will be bound)
  • x && y matches values which match both x and y
  • x where condition matches only if condition 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).

source
Rematch2.@matchMacro

Usage:

    @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.

source
Rematch2.@matchMacro

Usage:

    @match pattern = value

If value matches pattern, bind variables and return value. Otherwise, throw MatchFailure.

source
Rematch2.@match2Macro

Usage:

    @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.

source
Rematch2.@match2Macro

Usage:

    @match2 pattern = value

If value matches pattern, bind variables and return value. Otherwise, throw MatchFailure.

source
Rematch2.@match_failMacro
@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.

source
Rematch2.@match_returnMacro
@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.

source