Navigation
Synopsis Visiting tree structures and arbitrary values.
Description Visiting the elements of a data structure is one of the most common operations in our domain and the visitor design pattern is a solution known to every software engineer. Given a tree-like data structure we want to perform an operation on some (or all) nodes of the tree. The purpose of the visitor design pattern is to decouple the logistics of visiting each node from the actual operation on each node. In Rascal the logistics of visiting is completely automated.

Visiting is achieved by way of visit expressions that resemble the switch statement. A visit expression traverses an arbitrarily complex subject value and applies a number of cases to all its subtrees. All the elements of the subject are visited. When one of the cases matches the statements associated with that case are executed. These cases may:
  • cause some side effect, i.e., assign a value to local or global variables;
  • execute an Insert statement that replaces the current element;
  • execute a Fail statement that causes the match for the current case to fail (and undoing all side-effects due to the successful match itself and the execution of the statements so far).
The value of a visit expression is the original subject value with all replacements made as dictated by matching cases. The traversal order in a visit expressions can be explicitly defined by the programmer.
Examples Examples of visiting are, for instance, given in Recipes:ColoredTrees and Recipes:Derivative.
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.