Skip to main content

Variable Declaration Pattern

rascal-0.40.17

Synopsis

Variable declaration in abstract pattern.

Syntax

Type Var

Description

A variable declaration can be used as abstract pattern. A variable declaration introduces a new variable Var that matches any value of the given type Type and nothing else. That value is assigned to Var when the match succeeds.

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

Let's first perform a match that succeeds:

rascal>str S := "abc";
bool: true

and now we attempt to inspect the value of S:

rascal>S;
|prompt:///|(0,1,<1,0>,<1,1>): Undeclared variable: S
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UndeclaredVariable|
ok

As mentioned above: S is only bound in the scope of the match expression! Let's explore how bindings work in an if statement:

rascal>import IO;
ok
rascal>if (str S := "abc")
>>>>>>> println("Match succeeds, S == \"<S>\"");
Match succeeds, S == "abc"
ok