Pattern matching allows us to check if an object has some particular structure. For example, that a node of a binary tree has both children, or that it has a left child that has no children, and so on.
Pattern matching is performed by is operator that returns true if the pattern matched successfully and false otherwise. It has a negated form !is. The simplest form of pattern matching simply checks if an object conforms to a given type (similar to Java's instanceof check):
See smart casts.
Is and !is may be used as branch conditions in when expressions:
|When-expressions can replace switch-statement without pattern matching|
See more on when expressions here.
Patterns on the right-hand side of is and !is allow to match against types, constants, structure of tuples and other objects, and to bind objects matched by sub-patterns, to variables.
See the grammar for patterns.
|Complex patterns are deferred for future versions|
For now you can only use a type after is/!is. See the corresponding issue.