KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have unconstrained generic type Atomic which implements an initializer (details in my previous question ). type Atomic<T> = class type TFactory = reference to function: T; class function Initialize(var storage: T; factory: TFactory): T; end; Now I want to write simplified Initialize function which would take the type information from T (provided that typeof(T) is tkClass) and create new instance (when necessary) with the default constructor. Sadly, this fails: class function Atomic<T>.Initialize(var storage: T): T; begin if not assigned(PPointer(@storage)^) then begin if PTypeInfo(TypeInfo(T))^.Kind <> tkClass then raise Exception.Create('Atomic<T>.Initialize: Unsupported type'); Result := Atomic<T>.Initialize(storage, function: T begin Result := TClass(T).Create; // <-- E2571 end); end; end; Compiler reports error E2571 Type parameter 'T' doesn't have class or interface constraint . How can I trick the compiler to create an instance of class T?
Tags (comma-separated)
Save Edits
Cancel