Jump to content
A.M. Hoornweg

Strict type checking for tObject.

Recommended Posts

Hello all,

 

I saw how FreeAndNil is implemented in the RTL using a "CONST [ref]" parameter and then a little "tweak" is used to NIL the variable being referenced. I wondered why the heck the object wasn't simply passed as a VAR parameter in the first place. Until I tried that out and found that it wouldn't compile. Delphi insists that you either pass the exact object type or use a typecast.

 

What I don't understand is the reason why Delphi is so strict here; if a class derives from tObject, it still has all the original stuff in place from its ancestor so you can't really "break" anything by treating it as a tObject, so why is Delphi so strict?  It feels counter-intuitive.

 

Type tBase=Class(tObject)
  procedure test;
end;

Procedure tBase.Test;
begin
//
end;

Procedure MyFreeAndNil(VAR obj: tObject);
begin
 obj.free; 
 obj:=Nil;
end;


procedure test;
var t:tbase;
begin
  t:=tbase.create;
  MyFreeAndNil(t);
end;

 

Share this post


Link to post

It is pretty simple - imagine if the code below would work that way:

procedure ReplacePet(var pet: TPet);
begin
  pet.Free;
  pet := TCat.Create;
end;

procedure Main;
var
  dog: TDog;
begin
  ReplacePet(dog);
  dog.Bark; // meow?!
end;

FreeAndNil is special because it just destroys and assigns nil. But a var parameter does not give that guarantee.

Edited by Stefan Glienke
  • Haha 5

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×