![]() |
| ||||
Navigation |
Synopsis Declare a variable.
Syntax
Types
Description The effect of a variable declaration is to introduce a new variable
Name and
to assign the value of expression Exp to Name . A mention of Name later on in the same scope
will be replaced by this value, provided that Name s value has not been changed by an intermediate assignment.
When a variable is declared, it has as scope the nearest enclosing block, or the module when declared at the module level. The following rules apply:
Type Name;and only introduce the variable Name .
Rascal provides local type inference, which allows the implicit declaration of variables that are used locally in functions. The following rules apply:
Examples Two explicit variable declarations:
rascal>int max = 100; int: 100 rascal>min = 0; int: 0An implicit variable declaration rascal>day = {<"mon", 1>, <"tue", 2>, <"wed",3>, >>>>>>> <"thu", 4>, <"fri", 5>, <"sat",6>, <"sun",7>}; rel[str,int]: { <"tue",2>, <"thu",4>, <"sat",6>, <"mon",1>, <"sun",7>, <"fri",5>, <"wed",3> }Variable declaration and assignment leading to type error rascal>int month = 12; int: 12 rascal>month ="December"; |stdin:///|(7,10,<1,7>,<1,17>): Expected int, but got str
Pitfalls
![]() |