convert "class of" in pascal to D.

Piotr Szturmaj bncrbme at jadamspam.pl
Wed Jan 30 07:07:43 PST 2013


Eko Wahyudin wrote:
> Hi, guys? I'm new in D and I come from pascal. The constructor
> concept is very different, and it's very confusing for me.
>
> I want to create some statement in D which in pascal like this.
>
> type
>       TMyObjectAClass = class of TMyObject_A;   // number 1
>
>       TMyObject_A = class
>          constructor Create(AString : String);
>       end;
>
>       TMyObject_B = class(TMyObject_A)
>          constructor Create(AString : String); override;
>       end;
>
>       TMyObject_C = class(TMyObject_A)
>       end;
> - - - - - - - - - - - - - - - - -
>
> function Foo(AClass: TMyObjectAClass): TMyObject_A;
> begin
>      Result:= AClass.Create(AClass.ClassName);  //number 2
> end;
>
> - - - - - - - - - - - - - - - - -
> var
>      B : TMyObject_B;
>      C : TMyObject_C;
> begin
>      B:= Foo(TMyObject_B);  //number 3
>      C:= Foo(TMyObject_C);
> end.
>
> ===================================================================
> Question 1> How I can make a data type "class of" in D language
> Question 2> How to convert number 2, in D language.
> Question 3> How I passing a "class type" as argument like number
> 3, and if D can do same thing like pascal, which one is called by
> D, TMyObject_A.Create() or TMyObject_B.Create() ? In pascal It
> should be call TMyObject_B.Create() first

"class of" in Delphi/OP means type or subtype of a class. You can use 
template parameters in D to pass "class of":

// T : A means any type T that is equivalent to A or is subtype of A
A foo(T : A)()
{
     return new T(T.stringof);
}

B b;
C c;

b = foo!B();
c = foo!C();


More information about the Digitalmars-d mailing list