|
| |
| Navigation |
Synopsis Assign but replace if value is not defined.
Syntax
Assignable ? Exp2 = Exp1
Description First the value of
Exp1 is determined and if that is defined it is assigned to Assignable.
Otherwise, the value of Exp2 is assigned to Assignable. Values which can be undefined are values in Rascal/Expressions/Values/Maps where the key is not set or values of Annotations which have not been set yet.
No other values can be used in an undefined state, so the ? operator does not make sense on undefined or uninitialized variables for example.
Examples
rascal>M = ("Andy": 1, "Brian" : 2);
map[str, int]: ("Andy":1,"Brian":2)
Using an isDefined assignable can we increment a non-existing entry:
rascal>M["SomebodyElse"] ? 0 += 1; map[str, int]: ("Andy":1,"Brian":2,"SomebodyElse":1) rascal>M["SomebodyElse"]; int: 1And if we increment an existing entry the ? has no effect: rascal>M["Andy"] ? 0 += 1; map[str, int]: ("Andy":2,"Brian":2,"SomebodyElse":1) rascal>M["Andy"] int: 2 |