Class Type Parameters

doob doobnet at gmail.com
Thu Jan 17 08:42:58 PST 2008


Hans-Eric Grönlund wrote:
> 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
> 


I think you can, look here: 
http://www.digitalmars.com/d/1.0/template.html under Specialization


More information about the Digitalmars-d-learn mailing list