- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


Frequently Asked Questions

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);              { <- 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;



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile