In my shared codebase with Free Pascal, we dispose objects all over the place like this:
PROCEDURE DoDisposeObject (VAR anObject: TObject);
BEGIN
IF anObject <> NIL THEN
BEGIN
anObject.Free;
anObject := NIL;
END;
END;
VAR anItem := NEW Item();
DoDisposeObject(TObject(anItem));
This last line is illegal in Oxygene to ensure type safety.
Is there a recommended way to do this on Oxygene?
We could always do something like this:
FUNCTION DoDisposeObject (anObject: TObject): TObject;
BEGIN
IF anObject <> NIL THEN
anObject.Free;
EXIT NIL;
END;
VAR anItem := NEW Item();
anItem := DoDisposeObject(anItem);
But (1) it’s a little strange to do a method assignment for nil, and (2) it adds the risk of human error in not doing an assignment, e.g., just calling
DoDisposeObject(anItem);
instead of
anItem := DoDisposeObject(anItem);