UPDATE: Simple workaround. Data fields before Method/Operator fields.
Today I tried to reproduce this bug using the simplest example I could make.
I started with a basic record (TBasicRecord) having only simple set and print methods (no operators), and there was no problem passing const x:TBasicBecord.
I then added a unary operator thinking that would trigger the bug, but still no problems in passing the record as const.
I then added a binary operator, but and still the bug wouldn't surface.
Finally I noticed in my simple example I had declared the data fields ahead of the method fields, and this turned out to be all that's required to mute the bug.
I'd also made my data fields private, so at first I thought that must be the issue, but in the end it turned out to be irrelevant. The only thing that makes a difference is whether of not I placed the data fields before the operator and method fields.
Overall I'm happy with this resolution. Personally I've always put the data fields first anyway. It's funny that doing it the other way around didn't seem to cause any other problems, just as long as you don't try to pass the record type as a "const" parameter anywhere.
Original Posting:
Previously I have been using Delphi 7 but today installed Delphi 2006 to gain access to operator methods that D7 didn't support.
I was attempting to compile the code (complex number implementation) listed in one of the replies to an earlier question here: Request simple example of how to a TComplexMath class (source included)
Here's a partial listing of the relevant code:
type
TComplex = record
public
class operator Implicit(const D: Double): TComplex;
class operator Negative(const C: TComplex): TComplex;
class operator Equal(const C1, C2: TComplex): Boolean;
class operator NotEqual(const C1, C2: TComplex): Boolean;
class operator Add(const C1, C2: TComplex): TComplex;
class operator Add(const C: TComplex; const D: Double): TComplex;
class operator Add(const D: Double; const C: TComplex): TComplex;
class operator Subtract(const C1, C2: TComplex): TComplex;
class operator Subtract(const C: TComplex; const D: Double): TComplex;
class operator Subtract(const D: Double; const C: TComplex): TComplex;
class operator Multiply(const C1, C2: TComplex): TComplex;
class operator Multiply(const C: TComplex; const D: Double): TComplex;
class operator Multiply(const D: Double; const C: TComplex): TComplex;
class operator Divide(const C1, C2: TComplex): TComplex;
class operator Divide(const C: TComplex; const D: Double): TComplex;
class operator Divide(const D: Double; const C: TComplex): TComplex;
function IsZero: Boolean;
function IsNonZero: Boolean;
function Conj: TComplex;
function Sqr: TComplex;
function Sqrt: TComplex;
function Mag: Double;
function SqrMag: Double;
public
r: Double;
c: Double;
end;
class operator TComplex.Negative(const C: TComplex): TComplex;
begin
Result.r := -C.r;
Result.c := -C.c;
end;
---- etc ---
The problem is, when I try to compile this code (in D2006), every operator that takes a TComplex type gives an error of E2037: Declaration of "----" differs from the previous declaration. (where "---" is the operator name).
My work around was to just remove the const keyword from every TComplex parameter and then the code complies (and runs) correctly. I can keep the "const x: Double" parameters,the compiler gives no error on those, but I had to remove "const" from all of the others.
Does anyone know if this is some compiler option that's not enabled? Or is this something supported in later versions of Delphi but not D2006? Or just me doing something else incorrectly?
Also, if I cant use const parameters here, would there be any advantage to just substituting var for const (compared to just deleting the const keyword altogether).
var
forces the argument to be a variable, for one thing... – Andreas Rejbrand