* Posts by Craig Young

2 publicly visible posts • joined 5 Feb 2009

Born Again Delphi

Craig Young
Boffin

@Dave Murray

Actually, Verity's try..finally snippet is not flawed in the way that you think.

He has preinitialised his variables to nil before the try statement; therefore FreeAndNil will not attempt to destroy something that does not exist.

I agree that the normal convention is to Create before the try statement; however, Verity's convention is the only way to correctly support multiple objects protected by a single try..finally construct. Using the convention of construction before the try statement, you would have to nest multiple try..finally constructs, or face the risk of memory leaks when one object is created successfully, and the next is not.

Craig Young
Alert

C# new modifier is EVIL!!

First,

Delphi already has the exact same thing; it's called reintroduce.

Normally, if a subclass redeclares a *virtual* method without overriding (possibly also changing arguments), the Delphi compiler issues a warning because you are hiding the method in the ancestor class. By adding the reintroduce directive to the decalration, you can suppress the warning.

NOTE: If the method that you are 'hiding' is not virtual, then there is no need to provide a warning, because you are not impacting polymorphic behaviour.

Second,

Delphi provides the warning because it is DANGEROUS to hide a virtual method. It creates a situation where the implementation of a method that is invoked is no longer dependant solely on the class of the object for which it is invoked (the whole POINT of a virtual method); but is now ALSO dependant on the type of the reference to said object.

Moreover it is never necessary to hide a virtual method. Every example ever demonstrated to me could always be very easily rewritten to avoid the ambiguity.

In fact I classify "reintroduce" in the same category as "with". I would like it removed from the language; and for Delphi to report an error, if ever a virtual method is hidden; not merely a warning.