5.2. Assignment statements

Example 5-2. Examples:

a:=5
b(7).c:=5
A::d:=5
[3]:=5
e[7,8]:=5
g:INT:=5
h::=5
assign_statement ==>
        ( expression | identifier : [ type_specifier ] ) := expression

Assignment statements are used to assign objects to locations and can also declare new local variables. The expression on the right hand side must have a return type which is a subtype of the declared type of the destination specified by the left hand side. When a reference object is assigned to a location, only a reference to the object is assigned. This means that later changes to the state of the object will be observable from the assigned location. Since immutable and closure objects cannot be modified once constructed, this issue is not relevant to them. We consider each of the allowed forms for the lefthand side of an assignment in turn:

'identifier'

If the left hand side is a local variable or an argument of a method, then the assignment is directly performed (e.g. 'a:=5'). Otherwise the statement is syntactic sugar for a call of the routine named identifier with the right hand side of the assignment as the only argument (e.g. 'a(5)').

'( expression . | type_specifier :: ) identifier'

These forms are syntactic sugar for calls of a routine named identifier with the right hand side as an argument: ( expression . | type_specifier :: ) identifier ( rhs ). For example, 'b(7).c:=5' is sugar for 'b(7).c(5)' and 'A::d:=5' is sugar for 'A::d(5)'.

'[ expression ] [ expression_list ] '

This form is syntactic sugar for a call of a routine named 'aset' with the array index expressions and the right hand side of the assignment as arguments: [ expression . | type_specifier :: ] aset( expression_list , rhs ). For example, '[3]:=5' is sugar for 'aset(3,5)' and 'e[7,8]:=5' is sugar for 'e.aset(7,8,5)'.

'identifier : [ type_specifier ]'

This form both declares a new local variable and assigns to it (e.g. 'g:INT:=5'). If a type specifier is not provided, then the declared type of the variable is the return type of the expression on the righthand side (e.g. 'h::=5'). The scoping rules given on See Declaration statements apply here as well. If a type is explicitly specified, the construct is syntactic sugar for a declaration statement followed by an assignment statement.