Title: Getting stronger type checking
Question: How can I get Delphi to perform stronger type checking on user
defined types. Example: If I create a user defined type that
descends from a double, I can pass a variable of this new type
to any function that expects a double. I want Delphi to provide
stronger type checking and produce a warning in this instance.
Answer:
The following example demonstrates Delphi's new Strong type
checking types, allowing you to define types that require stronger type checking at compile time.
Example:
type TStrongType = type Double;
type TWeakType = Double;
procedure AddWeakType(var d : TWeakType);
begin
d := d + 1;
end;
procedure AddStrongType(var d : TStrongType);
begin
d := d + 1;
end;
procedure AddDoubleType(var d : Double);
begin
d := d + 1;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
d : Double;
s : TStrongType;
w : TWeakType;
begin
AddDoubleType(d); { compiles fine }
AddDoubleType(w); { compiles fine }
AddDoubleType(s); { AddDoubleType(double(s)); { compiles fine }
AddWeakType(d); { compiles fine }
AddWeakType(w); { compiles fine }
AddWeakType(s); { AddWeakType(TWeakType(s)); { compiles fine }
AddStrongType(d); { AddStrongType(TStrongType(d)); { compiles fine }
AddStrongType(w); { AddStrongType(TStrongType(w)); { compiles fine }
AddStrongType(s); { compiles fine }
end;