![]() |
|
Navigation |
Synopsis The static type system of Rascal.
Description Rascal is based on static typing, this means that as many errors and inconsistencies as possible are spotted before
the program is executed.
The Type LatticeThe types are ordered in a so-called type lattice shown in the following figure.![]() The arrows describe a subtype-of relation between types. The type void is the smallest type and
is included in all other types and the type value is the largest type that includes all other types.
We also see that rel is a subtype of set and that each ADT is a subtype of node .
A special role is played by the datatype Tree that is the generic type of syntax trees.
Syntax trees for specific languages are all subtypes of Tree . As a result, syntax trees can be addressed at two levels:
T1 and T2 , lub(T1, T2) is defined as the nearest common super type of T1 and T2
in the type lattice.
Advanced FeaturesThe Rascal type system has various advanced features that are described separately:
Examples Here are some simple examples of correct and incorrect typing:
We can assign an integer value to an integer variable:
rascal>int i = 3;
int: 3
But assigning a string value gives an error:
rascal>int j = "abc";
|stdin:///|(4,9,<1,4>,<1,13>): Expected int, but got str
The num type accepts integer and real values:
rascal>num n = i; num: 3 rascal>n = 3.14; num: 3.14A variable of type value accepts all possible values:
rascal>value v = true; value: true rascal>v = "abc"; value: "abc" rascal>v = [1, 2, 3]; value: [1,2,3] ![]() |