Skip to main content

Literal Pattern

rascal-0.34.0

Synopsis

Literal in abstract pattern.

Syntax

"string"
123
1.0
|http://www.rascal-mpl.org|

Description

A literal of one of the basic types Boolean, Integer, Real, Number, String, Location, or DateTime can be used as abstract pattern. A literal pattern matches with a value that is identical to the literal.

Examples

A literal pattern matches with a value that is equal to it:

rascal>123 := 123
bool: true
rascal>"abc" := "abc"
bool: true

A literal pattern does not match with a value that is not equal to it:

rascal>123 := 456
bool: false
rascal>"abc" := "def"
bool: false

If the type of the literal pattern is incomparable to the subject's type, a static type error is produced to announce that the match is guaranteed to fail:

rascal>123 := "abc";
|prompt:///|(7,5,<1,7>,<1,12>): Expected int, but got str
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UnexpectedType|
ok

However, a literal pattern can be used to filter among other values:

rascal>value x = "abc";
value: "abc"
rascal>123 := x;
bool: false
rascal>x = 123;
value: 123
rascal>123 := x;
bool: true