Skip to main content

Typed and Labelled Pattern

rascal-0.34.0

Synopsis

Typed, labelled, abstract pattern.

Syntax

Type Var : Pattern

Description

A typed, labelled, pattern matches when the subject value has type Type and Pattern matches. The matched value is assigned to Var.

This construct is used for:

  • binding the whole pattern to a variable while also matching some stuff out of it: MyType t : someComplexPattern(f(int a), int b)). This is similar to Labelleds patterns but with an extra type
  • to assert that the pattern has a certain type. This can be useful in disambiguating a constructor name, as in the example below.

Examples

rascal>import IO;
ok
rascal>data Lang = add(Lang l, Lang r) | number(int i);
ok
rascal>data Exp = id(str n) | add(Exp l, Exp r) | subtract(Exp l, Exp r) | otherLang(Lang a);
ok
rascal>ex = add(id("x"), add(id("y"), otherLang(add(number(1),number(2)))));
Exp: add(
id("x"),
add(
id("y"),
otherLang(add(
number(1),
number(2)))))
rascal>visit (ex) {
>>>>>>> case Lang l:add(_,_) : println("I found a Lang <l>");
>>>>>>> case Exp e:add(_,_) : println("And I found an Exp <e>");
>>>>>>>}
I found a Lang add(number(1),number(2))
And I found an Exp add(id("y"),otherLang(add(number(1),number(2))))
And I found an Exp add(id("x"),add(id("y"),otherLang(add(number(1),number(2)))))
Exp: add(
id("x"),
add(
id("y"),
otherLang(add(
number(1),
number(2)))))