views:

63

answers:

5

I'm not sure if this is possible in Delphi. I've looked around and can't seem to find an answer (Example or inidaction that it's not possible):

I have a generic list class, and I want to create an instance of the generic type. For example:

type
  TTypeA = class(TObject);

procedure Test;
var
  MyList: TobjectList<TTypeA>;
  NewListObject: TTypeA;
begin
  MyList := TObjectList<TTypeA>.Create;
  NewListObject := MyList.xxx //what to put on the xxx
end;

Is it possible to create a function xxx that creates a new object of the type TTypeA?

@jeroen: thanks for the answer below. However, I forgot an important detail in my question:

I would like this code to work for any other type as well, so without prior knowledge about the type T for TObjectList. I might create the following lists:

MyList: TObjectList<TCar>;
MyList: TObjectList<TBike>;

Without knowing if MyList contains TCar or TBike (both derived from the same base class and equal constructors) I want to add a new item to MyList.

And with the suggestion from Uwe Raabe I run into the next problem:

I modified my class to

TMyObjectList<T:class, constructor> = class(TMyBaseObjectList<T>)

where TMyBaseObjectList is defined as

TMyBaseObjectList<T:TMyBaseObject> = class(TObjectList)

Now I get an error: Type parameter 'T' is not compatible with type 'T:TMyBaseObject'

A: 

At the xxx, put this: TTypeA.Create(...); where ... are the parameters to your create constructor.

--jeroen

Jeroen Pluimers
A: 

My guess is that it isn't possible. Unless of course you have items in your list, in that case you can do:

  NewListObject := MyList[0].ClassType.NewInstance as TTypeA;
Ville Krumlinde
Bad idea to call NewInstance directly. It doesn't call the constructor.
Mason Wheeler
I didn't realize that, thanks for pointing it out. Anyway Uwes answer looks like the best solution.
Ville Krumlinde
+1  A: 

As you know the type, why don't you just write

NewListObject := TTypeA.Create;
MyList.Add(NewListObject);

It would be the natural way to do here. (I'm just notified by Jeroen's similar answer)

If you want the container create the object, you have to make a descendant class that knows a little bit more about the generic type to create an instance. The constructor constraint might help here.

type
  TMyObjectList<T:class, constructor> = class(TObjectList<T>)
  public
    function NewObject: T;
  end;

function TMyObjectList<T>.NewObject: T;
begin
  result := T.Create;
end;

Note: This will only work when the actual type has a parameterless constructor named Create.

Update: This will help

  TMyObjectList<T:constructor, TMyBaseObject> = class(TObjectList<T>)
  public
    function NewObj: T;
  end;
Uwe Raabe
A: 

Alternatively, you could descend your list class from TCollection and your items from TCollectionItem and use those as base classes. They are built with all that plumbing already. To use your example:

type
    TTypeA = class(TCollectionItem);

procedure Test;
var
    MyList: TCollection;
    NewListObject: TTypeA;

begin
    MyList := TCollection.Create(TTypeA);
    NewListObject := MyList.Add;
end;

Edit: I know, it's not using Generics, but you could look to TCollection and TCollectionItem for more design ideas, particularly when it comes to dealing with ownership, notifications, and lifetime management.

afrazier
A: 

As far as I know, you can only use parameterless constructor inside the generic class on your generic type.

One "workaround" for this problem is to use an anonymous method to create the object, but that means you will need to create many methods.

As for the other problem...

TMyObjectList<T:class, constructor> = class(TMyBaseObjectList<T>)
TMyBaseObjectList<T:TMyBaseObject> = class(TObjectList)

I believe the proper declaration would be

TMyObjectList<T:TMyBaseObject, constructor> = class(TMyBaseObjectList<T>)
TMyBaseObjectList<T:TMyBaseObject> = class(TObjectList)

TMyBaseObjectList already enforce T to be of type TMyBaseObject. If you declare T:class in TMyObjectList, it won't satisfy TMyBaseObjectList's requirement.

Ken Bourassa