| |
| Getting stronger type checking |
 |
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.
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 + ;
end;
procedure AddStrongType(var d : TStrongType);
begin
d := d + ;
end;
procedure AddDoubleType(var d : Double);
begin
d := d + ;
end;
procedure TForm .Button Click(Sender: TObject);
var
d : Double;
s : TStrongType;
w : TWeakType;
begin
AddDoubleType(d); { compiles fine }
AddDoubleType(w); { compiles fine }
AddDoubleType(s); { <- compile error }
AddDoubleType(double(s)); { compiles fine }
AddWeakType(d); { compiles fine }
AddWeakType(w); { compiles fine }
AddWeakType(s); { <- compile error }
AddWeakType(TWeakType(s)); { compiles fine }
AddStrongType(d); { <- compile error }
AddStrongType(TStrongType(d)); { compiles fine }
AddStrongType(w); { <- compile error }
AddStrongType(TStrongType(w)); { compiles fine }
AddStrongType(s); { compiles fine }
end;
|
|
| Hits/month |
2,500,000+ |
Downloads (Since May 2000) |
7,393,709 |
| Total Files |
6,023 |
| Forum msgs |
7,670 |
| Articles/FAQs |
70+/900+ |
Top Selling Software at Amazon
|