Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. It is a more powerful version of the switch statement in PHP and it can likewise be used in place of a series of if/else statements.
This pre-processor will translate the operations using the pattern matching syntax below into pure PHP. This does not happen at runtime, so there is no hit on perfomance. The pre-processor works on top of the pre plugin and the yay macro library.
- Simplest pattern matching syntax example
- Catch all using a
case _
syntax - Case classes support
- Guards support
- Check for exhausting clauses error (sealed number of cases)
- List deconstruction
- Case class deconstruction
$y = $x match {
1 => "one",
2 => "two"
}
$y = $x match {
1 => "one",
2 => "two",
_ => "many"
}
- This pre-processor is alpha software. Do not use it in production.