Skip to main content

Variable Pattern

rascal-0.34.0

Synopsis

Variable in abstract pattern.

Syntax

Var

Description

A variable pattern can act in two roles:

  • If Var has already a defined value in the current scope then it matches with that value. This means an equality check of the bound value against the subject that is matched against. Note that the presence or absence of keyword fields anywhere in the subject or the bound value is ignored.

  • If Var has not been defined before (or it has been declared but not initialized) then it matches any value. That value is assigned to Var. The scope of this variable is the outermost expression in which the pattern occurs or the enclosing If, While, or Do if the pattern occurs in the test expression of those statements.

Examples

Initialize variable N

rascal>N = 10;
int: 10

and use N in a pattern; its value is used as value to match with:

rascal>N := 10;
bool: true
rascal>N := 20;
bool: false

Use a non-existing variable in a pattern, it is bound when the match succeeds:

rascal>import IO;
ok
rascal>if(M := 10)
>>>>>>> println("Match succeeded, M == <M>");
Match succeeded, M == 10
ok