haxe Pattern matching Extractors


Example

Extractors are identified by the extractorExpression => match expression. Extractors consist of two parts, which are separated by the => operator.

  1. The left side can be any expression, where all occurrences of underscore _ are replaced with the currently matched value.
  2. The right side is a pattern which is matched against the result of the evaluation of the left side.

Since the right side is a pattern, it can contain another extractor. The following example "chains" two extractors:

static public function main() {
    switch(3) {
        case add(_, 1) => mul(_, 3) => a:
            trace(a); // mul(add(3 + 1), 3)
    }
}

static function add(i1:Int, i2:Int) {
    return i1 + i2;
}

static function mul(i1:Int, i2:Int) {
    return i1 * i2;
}

It is currently not possible to use extractors within or-patterns. However, it is possible to have or-patterns on the right side of an extractor.

References