Of the things not possible with generics in Delphi, I already wrote about Impossible: Property using Generics in Delphi.
Now it’s time to write about simulating a free function with generics as this is impossible:
function CreateManaged<T>(const value: T): IManaged<T>;
Usually I create a static record method for these, as records do not have VMT overhead, less RTTI overhead and I’ve been used to see records as helpers for other types long before helpers initially were introduced in Delphi 8 to extend classes..
Spring4D has an excellent example of such a record workaround in the Managed.New<T>
commit:
type
Managed = record
public
class function New<T>(const value: T): IManaged<T>; static;
end;
...
class function Managed.New<T>(const value: T): IManaged<T>;
begin
Result := TManaged<T>.Create(value);
end;
It basically is a factory for classes implementing the IManaged
interface.
In the mean time, Managed
has been renamed to Shared
but the above code shows my point.
–jeroen
Reference: [WayBack] Class Helpers documented in Delphi 2007, introduced in Delphi 8
Like this:
Like Loading...