Class Type Parameters

Hans-Eric Grönlund hasse42g at gmail.com
Thu Jan 17 08:19:23 PST 2008


Cool, not exactly what I was looking for, but I can use it to solve the problem - a little differently than I'd expected.

Just out of curiosity: In Delphi (Object Pascal) one can assign a class type to a variable, like so:

type 
TAClass = class
  ...
end;

TAClassClass = class of TAClass;

procedure do_something_with_class(AClass: TAClassClass);
begin
  ...
end;

Can something similar be done in D?


bearophile Wrote:

> Hans-Eric Grönlund:
> > How could I declare a class type parameter in D? I'd like to be able to do
> > something like this:
> > do_something_with_class(SomeClass);
> > How would I declare the do_something_with_class function?
> 
> Do you mean something like this?
> 
> import std.stdio;
> 
> class C1 { int i; }
> class C2 { float f; }
> 
> void manage(TyClass)() {
>     auto c = new TyClass;
> 	writefln(c);
> }
> 
> void main() {
>     manage!(C1);
>     manage!(C2)();
> }
> 
> Note that manage is a templated function.
> D types seem almost first-class things, so you can manage them, with care.
> 
> Bye,
> bearophile



More information about the Digitalmars-d-learn mailing list