Assignment Operators
The assignment operator (=) is used to assign new values to mutable variables declared with var. An important safety design in Cangjie is that the assignment expression has type Unit with value (), not the assigned value itself. This effectively prevents the common error of mistyping the equality operator == as the assignment operator =, for example if (a = 5) will result in a compile-time error.
Cangjie does not support chained assignment (like a = b = 0). During assignment, the expression on the right side of = is always evaluated first, then the expression on the left side, and finally the assignment is performed. This ensures predictable evaluation order.
Cangjie provides compound assignment operators, including +=, -=, *=, /=, %=, **=, &&=, ||=, etc., which can simplify code. Compound assignment expressions also require operands to have the same type, and the expression type is also Unit. Compound assignment operators are also non-associative and cannot be used consecutively.
The code on the right demonstrates the use of basic assignment and compound assignment. You can try rewriting compound assignment as regular assignment to compare the two styles.
Unit is a Cangjie safety feature, avoiding the common if (a = 5) error in C/Java. Such errors will directly cause compilation failure in Cangjie.