Главная страница | назад





Article #17126: Getting stronger type checking

 Question and Answer Database
FAQ2126D.txt Getting stronger type checking
Category :VCL
Platform :All
Product :All 32 bit
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); { <- 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;
7/16/98 4:31:28 PM

Last Modified: 01-SEP-99