The de nition of an arithmetic operator takes two parameters. The rst parameter must be of the type that occurs at the left of the operator, the second parameter must be of the type that is at the right of the arithmetic operator. The result type must match the type that results after the arithmetic operation.
To compile an expression as
var
R : real; C,Z : complex; begin C:=R*Z; end; |
one needs a de nition of the multiplication operator as:
Operator * (r : real; z1 : complex) z : complex;
begin z.re := z1.re * r; z.im := z1.im * r; end; |
As can be seen, the rst operator is a real, and the second is a complex. The result type is complex.
Multiplication and addition of reals and complexes are commutative operations. The compiler, however, has no notion of this fact so even if a multiplication between a real and a complex is de ned, the compiler will not use that de nition when it encounters a complex and a real (in that order). It is necessary to de ne both operations.
So, given the above de nition of the multiplication, the compiler will not accept the following statement:
var
R : real; C,Z : complex; begin C:=Z*R; end; |
since the types of Z and R don't match the types in the operator de nition.
The reason for this behaviour is that it is possible that a multiplication is not always commutative. e.g. the multiplication of a (n,m) with a (m,n) matrix will result in a (n,n) matrix, while the mutiplication of a (m,n) with a (n,m) matrix is a (m,m) matrix, which needn't be the same in all cases.