Oxygene VAR/OUT param inheritance

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

Quite

The best option I can think of would be something like

PROCEDURE DoDisposeObject<T>(VAR anObject: T);
1 Like

Ah yes. I’m getting an “unknown type” error on “T” in that snippet though… Is that the correct syntax for generics on a method?

Oops my bad. I had it defined in the interface as well and didn’t change it there.

1 Like