Compound Assignments
In addition to regular assignments (that is, :=), the Jade language supports compound assignments.
Syntax
Left-hand-operand assignment-operator expression;
The compound assignment operators listed in the following table are supported.
Compound Assignment Operator | Assignment |
---|---|
+= | Addition |
-= | Subtraction |
*= | Multiplication |
/= | Division |
^= | Power |
%= | Modulo |
&= | Concatenation |
Description
Compound assignment operators are an extension to the := assignment operation. They provide a shorthand syntax for assigning the result of an arithmetic or concatenation operation.
Compound assignment operators perform the operation specified by the additional operator on the left and right operands, then assign the result to the left operand. The following is therefore equivalent to the above syntax.
Left-hand-operand := left-hand-operand operator (expression);
The expression is in parentheses because the additional arithmetic operation is performed on the left‑hand operand and the result of the expression, before assigning it to the left‑hand operand.
Rules
Compound assignment operators have strict rules about the types that can be used for the various operators, as follows.
-
The left-hand operand must be an assignable value. This is the same as simple assignment statements.
-
The left‑hand and right‑hand operands must both be compatible with the additional operator.
-
The resultant type of the arithmetic or concatenation operation must be assignment‑compatible with the left‑hand operand.
-
When using an arithmetic assignment operator, the left‑hand operand must be a numeric type. These are:
-
Byte
-
Date
-
Decimal
-
Integer64
-
Integer
-
Real
-
TimeStampInterval
-
TimeStampOffset
-
Time
-
-
When using the concatenation assignment operator, the left‑hand operand must be one of the following types.
-
String
-
StringUtf8
-
Binary
-
Subtleties
To provide a straightforward syntax, integer division (div) and real division (/) operators have both been incorporated into the division assignment (/=) operator. The operation that gets performed depends on the type of the left‑hand operand.
-
Integer or Integer64 left‑hand operand types result in integer division.
-
Real, Decimal, or TimeStampInterval left‑hand operands result in real division.
-
All other types cannot be used with the division assignment operator as the behavior is either undefined or the resulting type does not match the left‑hand operand.
This combined division assignment operator approach means that it is not possible to short‑hand a Real, Decimal, or TimeStampInterval integer division, as it always favors real division.
Example
The following demonstrates some valid compound assignment examples.
vars integer : Integer; real : Real; string : String; begin integer := 6; integer += 94; integer -= 50; integer /= 10; write integer; real := 123.456; real /= 100; write real; string := "hello"; string &= " world!"; write string; end;
The above example outputs the following.
5 1.23456 hello world!
2020.0.02 and higher