It is possible. Create an interface that provides the methods / objects you want:
type IStringList = interface procedure Add(const s: string);
Deploy the interface and wrap the real TStringList :
type TStringListImpl = class(TInterfacedObject, IStringList) private FStringList: TStringList; // create in constructor, destroy in destructor // implementation etc. end;
Then write:
type TStringListRecord = record private FImpl: IStringList; function GetImpl: IStringList; // creates TStringListImpl if FImpl is nil // returns value of FImpl otherwise public procedure Add(const s: string); // forward to GetImpl.Add property StringList: TStringList read GetStringList; // forward to // GetImpl.StringList // etc. end;
The fact that there is an interface inside the record means that the compiler automatically processes the reference count, calling _AddRef and _Release, since the copies are created and destroyed, so the life cycle is controlled automatically. This works for objects that will never contain a reference to themselves (loop creation). To tally the links, you need different tricks to cycle through the loops in the reference graph.
source share