103 Variable Updates

Numerical Variables


status := 25;
status++;
status += 5;
qty--;
qty -= 2;
weight *= 1000;
ratio /= 2;
score ^= 2;

An update target can be an ordinary variable. If status has numerical type, := replaces its value, ++ increments it, and += adds the right-hand value to it. The other compound numerical operators have the same shape: they read the current value, apply the corresponding arithmetic operator, and write the result back to the selected target.

When the selected target is an ordinary program variable, the update is a local program-state update. It does not require a database transaction. The same operators may also be used for database-state locations selected by paths; those updates are governed by the transaction rules.

Update Meaning
T := E replace the selected value of T with E
T++ T := T + 1
T-- T := T - 1
T += E T := T + E
T -= E T := T - E
T *= E T := T * E
T /= E T := T / E
T ^= E T := T ^ E

Each operator is defined only when the corresponding expression is defined and can be assigned to the selected target. Plain assignment may initialise an uninitialised target. Every compound update reads its target before writing and therefore requires it to be definitely initialised; see Uninitialised Locations.

String Variables


name := "Smith";
name += " and Sons";
name[0:0] := "Mr ";
name[:3] := "Dr ";
name[3:8] := "Smythe";
name[3:8] := "";
name[-4:] := "Ltd";
name[-1:-1] := "!";

For a string variable, += appends the right-hand string. Slice assignment updates the selected substring. The slice form follows Python's half-open convention: S[i:j] selects positions from i up to but not including j; omitted bounds mean the start or end of the string; negative indices count from the end.

Assigning to an empty slice inserts text at that position. Assigning the empty string to a non-empty slice deletes the selected text. Assigning any other string to a non-empty slice replaces the selected text.

For example, name[:3] selects the prefix ending before index 3, name[-4:] selects the suffix beginning four characters from the end, and name[-1:-1] is an empty slice immediately before the final character.

Enumerated Variables


color := PrimaryColor.red;
color := PrimaryColor.blue;

An enumerated variable supports assignment. The assigned value must be a value of the enumerated type, or of a subtype whose values are valid for that enumerated type.

Variant Variables


shape := newShape;
shape.circle.radius += 1;

A variant variable supports assignment at the variant type. A variant also has named attributes, so operations that depend on the payload of a particular attribute use ordinary attribute navigation. In the example, shape.circle selects the location of the circle payload before updating radius. If shape is not currently a circle, the path selects no locations and the update performs no writes.

Union Variables


value := newValue;
value.as<Circle>.radius += 1;

A union value does not have a variant attribute that can be used as a path step. When an update needs to drill into a particular member type of a union, the type is the handle. The expression value.as<Circle> casts the location to the Circle member before updating radius. If value is not currently a Circle, the path selects no locations and the update performs no writes.

Operator Validity

Update operators are type-directed. Numerical variables and numerical locations support :=, ++, --, +=, -=, *=, /=, and ^=. String variables and string locations support :=, +=, and slice assignment. Enumerated variables and locations support assignment only.

Variant variables and locations support assignment at the variant type; named variant attributes are ordinary path navigation. Union variables and locations support assignment at the union type; as<T> is used when the member type is the only handle for drilling down into the selected value.