On Wednesday, 30 January 2013 at 16:16:49 UTC, Eko Wahyudin wrote:
thanks all,

I think i am understand, if the closest solution is template,
there is no solution in D for my code like this.

type
      TMyObjectClass = class of TMyObjectA;

var
      ClassArray : array of TMyObjectClass; //the content
initialized
randomly
      ObjectArray : array of TMyObjectA;
      i, j : Integer;

procedure CreateAllObject;
begin
      J:= length(ClassArray);
      for I:= 0 to J do begin
        ObjectArray[I]:= ClassArray[I].Create({some argument});
      end;
end;

if D unable to do this, I think we must do something with D.

I'm still thinking, why constructor in D adopt C++ style rather
than pascal style, while pascal style is better :-?


The only problem for translating your code is the parameterized constructor. Otherwise, your code will look in D like this an no template is involved:

TypeInfo_Class[] ClassArray;
TMyObjectA[] ObjectArray;

void CreateAllObject()
{
    foreach(int i = 0; i < ClassArray.length; i++)
       ObjectArray[i] = ClassArray[i].create();
}

Anyway, you can transform your constructor parameter in a field or property and set it in the for statement for each new object.

Reply via email to