The assignment operator defines the action of a assignment of one type of variable to another. The result type must match the type of the variable at the left of the assignment statement, the single parameter to the assignment operator must have the same type as the expression at the right of the assignment operator.
This system can be used to declare a new type, and define an assignment for that type. For instance, to be able to assign a newly defined type “Complex”
The following assignment operator would have to be defined:
To be able to assign a real type to a complex type as follows:
the following assignment operator must be defined:
As can be seen from this statement, it defines the action of the operator := with at the right a real expression, and at the left a complex expression.
An example implementation of this could be as follows:
As can be seen in the example, the result identifier (z in this case) is used to store the result of the assignment. When compiling in Delphi mode or ObjFPC mode, the use of the special identifier Result is also allowed, and can be substituted for the z, so the above would be equivalent to
The assignment operator is also used to convert types from one type to another. The compiler will consider all overloaded assignment operators till it finds one that matches the types of the left hand and right hand expressions. If no such operator is found, a “type mismatch” error is given.
Remark The assignment operator is not commutative; the compiler will never reverse the role of the two arguments. In other words, given the above definition of the assignment operator, the following is not possible:
If the reverse assignment should be possible then the assignment operator must be defined for that as well. (This is not so for reals and complex numbers.)
Remark The assignment operator is also used in implicit type conversions. This can have unwanted effects. Consider the following definitions:
Then the following assignment will give a type mismatch:
The mismatch occurs because the compiler will encounter the definition of the exp function with the complex argument. It implicitly converts r2 to a complex, so it can use the above exp function. The result of this function is a complex, which cannot be assigned to r1, so the compiler will give a “type mismatch” error. The compiler will not look further for another exp which has the correct arguments.
It is possible to avoid this particular problem by specifying
When doing an explicit typecast, the compiler will attempt an implicit conversion if an assignment operator is present. That means that
Will be handled by an operator
However, an Explicit operator can be defined, and then it will be used instead when the compiler encounters a typecast.
The reverse is not true: In a regular assignment, the compiler will not consider explicit assignment operators.
Given the following definitions:
It is possible to create assignment operators:
But one can also define typecasting operators:
Thus, the following code
will produce the following output: