Posts

Showing posts from March, 2019

Inline Variables Type Inference And Reference Counted Classes

There are two (OK, three) features I have been eagerly waiting for in Delphi.
The ability to have multiple class helpers (aka extension methods) in scopeInline variables and (3.) type inference While I will have to wait some more for the former, Delphi Rio introduced the latter, making me a very happy camper.

But it is not all perfect. Type inference does not work on reference counted classes. Or rather, it does work, but it breaks reference counting.

For example, this inline variable declaration:
var Ref := TInterfacedObject.Create; will be translated by the compiler into:
var Ref: TInterfacedObject := TInterfacedObject.Create;
In the above code, the compiler correctly infers the type - TInterfacedObject, but by doing so, it stores a reference counted object in an object reference. And as we all know, this kind of code breaks the reference counting mechanism. The famous "Don't mix objects and interfaces" rule.

To properly initialize reference counting, you have to expl…