Skip to main content

Pattern With Action



A pattern with an associated action that is executed on a successful match.


  • Pattern => Exp
  • Pattern: Statement


Patterns can be used in various contexts, but a common context is a PatternWithAction, which in its turn, may be used in various statements such Switch and Visit.

There are two variants as listed above:

  • When the subject matches Pattern, the expression Exp is evaluated and the subject is replaced with the result.

  • When the subject matches Pat, the Statement is executed. More statements can be executed by including them in a Block.

In Switch statements, only the form Pattern : Statement is allowed. When the subject matches Pattern, the Statement is executed and the execution of the switch statement is complete. However, when a fail statement is executed in Statement further alternatives of Pattern are tried. If no alternatives remain, PatternWithAction as a whole fails and subsequent cases of the switch statement are tried.

In Visit expressions, the form Pattern => Exp describes subtree replacement: the current subtree of the subject of the visit expression is replaced by the value of Exp. The form Pattern : Statement is as described for switch statements, with the addition that execution of an Insert statement will replace the current subtree. After both success or failure of the PatternWithAction, the traversal of the subject continues.


Two examples of variant 1 (replacement):

case red(CTree l, CTree r) => red(r,l)
case red(l, r) => green(l, r)

Three examples of variant 2 (Statement):

case /Leila/: println("The topic is Starwars");
case red(_, _): println("A red root node");
case red(_,_): c = c + 1;

The action may also be a Block:

case red(_,_): { c = c + 1; println("c = <c>"); }