Functional pattern matching
The functional pattern matching it's an extension of the multiple dispatch feature, and allows us to call a certain function or method using values or expressions as patterns, instead of types.
Example:
Taking advantage of this feature, we can write the Fibonacci function in the following way:
In addition, instead of an expression, we can specify a block of code, which will get called with the value of the argument for checking:
For keeping the value of the argument, we can specify a parameter name in front of the block used for pattern matching:
Pattern matching is available for methods as well:
Last updated