I’m checking out the Delphi 2009 Trial, but run into problems with the generics stuff right away.
The following code does not compile, and I haven’t the slightest idea why it’s giving me E2015 for the Equals() method:
type TPrimaryKey<T> = class(TObject) strict private fValue: T; public constructor Create(AValue: T); function Equals(Obj: TObject): boolean; override; function GetValue: T; end; constructor TPrimaryKey<T>.Create(AValue: T); begin inherited Create; fValue := AValue; end; function TPrimaryKey<T>.Equals(Obj: TObject): boolean; begin Result := (Obj <> nil) and (Obj is TPrimaryKey<T>) and (TPrimaryKey<T>(Obj).GetValue = fValue); end; function TPrimaryKey<T>.GetValue: T; begin Result := fValue; end;
Why does the compiler think that fValue and the result of GetValue() can not be compared?
What if T is a string? What if it’s a TSize record?
Without constraining T (e.g. with <T :class>), you can’t be sure that the comparison will be meaningful.
If, instead, you wanted to compare two values of type T, you can use the Generics.Defaults unit and use:
to compare values x and y of type T.