Generics: What's a “CONSTRUCTOR constraint”?

霸气de小男生 提交于 2020-01-11 01:40:27

问题


I made a custom TObjectList descendant designed to hold subclasses of a base object class. It looks something like this:

interface
   TMyDataList<T: TBaseDatafile> = class(TObjectList<TBaseDatafile>)
   public
      constructor Create;
      procedure upload(db: TDataSet);
   end;

implementation

constructor TMyDataList<T>.Create;
begin
   inherited Create(true);
   self.Add(T.Create);
end;

I want each new list to start out with one blank object in it. It's pretty simple, right? But the compiler doesn't like it. It says:

"Can't create new instance without CONSTRUCTOR constraint in type parameter declaration" I can only assume this is something generics-related. Anyone have any idea what's going on and how I can make this constructor work?


回答1:


You're trying to create an instance of T via T.Create. This doesn't work because the compiler doesn't know that your generic type has a parameterless constructor (remember: this is no requirement). To rectify this, you've got to create a constructor constraint, which looks like this:

<T: constructor>

or, in your specific case:

<T: TBaseDatafile, constructor>



回答2:


Just a quick update to an old question..

You don't need the constructor constraint and can also do this on object's with parameters by using RTTI like this (using RTTI or System.RTTI with XE2)

constructor TMyDataList<T>.Create;
var
  ctx: TRttiContext;
begin
   inherited Create(true);
   self.Add(
     ctx.
     GetType(TClass(T)).
     GetMethod('create').
     Invoke(TClass(T),[]).AsType<T>
   );
end;

If you have parameters, just add them like this

constructor TMyDataList<T>.Create;
var
  ctx: TRttiContext;
begin
   inherited Create(true);
   self.Add(
     ctx.
     GetType(TClass(T)).
     GetMethod('create').
     Invoke(TClass(T),[TValue.From('Test'),TValue.From(42)]).AsType<T>
   );
end;


来源:https://stackoverflow.com/questions/383689/generics-whats-a-constructor-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!