Hi Folks!
The following operator calls itself recursive:
program Operator1;
operator + (a, b: Integer) = c: Integer; begin WriteLn ('Play it again, Sam.'); c := 2 * a + b end;
begin WriteLn ('2 + 3 = ', 2 + 3) end.
Is there a way to tell my new operator, which '+' should be used in the operator's body? I know, I could cast or use other types in the operator's body, but the code would become bloated. Are user-defined operators intended to be used with user-defined types only?
Eike
Eike Lange wrote:
The following operator calls itself recursive:
program Operator1;
operator + (a, b: Integer) = c: Integer; begin WriteLn ('Play it again, Sam.'); c := 2 * a + b end;
begin WriteLn ('2 + 3 = ', 2 + 3) end.
Is there a way to tell my new operator, which '+' should be used in the operator's body? I know, I could cast or use other types in the operator's body, but the code would become bloated. Are user-defined operators intended to be used with user-defined types only?
Mostly, I think so. But the current implementation of operator overloading was done by Peter. I haven't changed anything substiantial in it, and haven't used it much myself, so I can only guess like you do.
See test/kohl.pas (also by Peter) for another possible "work-around" in this special case:
operator + (x, y: Integer) z: Integer; begin z:= x; Inc (z, y); Dec (z) end;
Another way would be to define a routine which calls the original operator before the new one is declared. Also bloat, of course, but if it's inlined, maybe only syntactically ...
Frank