Thread overview | |||||
---|---|---|---|---|---|
|
March 15, 2008 Base interfaces tuple | ||||
---|---|---|---|---|
| ||||
I've been trying to write a template that returns a tuple of all base interfaces from either a derived class or interface. D2 has InterfacesTuple, but it doesn't work on interfaces, nor does it return a complete list of base types. Here's what I'm aiming for: interface I1 {} interface I2 : I1 {} interface I3 : I2 {} interface I4 : I3 {} writefln(typeid(AllBaseInterfaces!(I4)); Which should produce: (I1,I2,I3) BaseTypeTuple from std.traits seems to be the starting point, and recursively calling that template gets successive base types, but putting that together into a single template has defeated me. Anyone accomplished this? Cheers, John. |
March 15, 2008 Re: Base interfaces tuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to John C | "John C" <johnch_atms@hotmail.com> wrote in message news:frgcqg$10kl$1@digitalmars.com... > BaseTypeTuple from std.traits seems to be the starting point, and recursively calling that template gets successive base types, but putting that together into a single template has defeated me. Anyone accomplished this? Here's a Tango-y version: import tango.core.Traits; template AllBasesImpl(T...) { static if(T.length == 0) alias Tuple!() AllBasesImpl; else alias Tuple!(T[0], AllBasesImpl!(BaseTypeTupleOf!(T[0])), AllBasesImpl!(T[1 .. $])) AllBasesImpl; } template AllBases(T) { alias Unique!(AllBasesImpl!(BaseTypeTupleOf!(T))) AllBases; } I believe the Phobos version would be something like this: import std.typetuple; import std.traits; template AllBasesImpl(T...) { static if(T.length == 0) alias TypeTuple!() AllBasesImpl; else alias TypeTuple!(T[0], AllBasesImpl!(BaseTypeTuple!(T[0])), AllBasesImpl!(T[1 .. $])) AllBasesImpl; } template AllBases(T) { alias NoDuplicates!(AllBasesImpl!(BaseTypeTuple!(T))) AllBases; } |
March 15, 2008 Re: Base interfaces tuple | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley Wrote:
> "John C" <johnch_atms@hotmail.com> wrote in message news:frgcqg$10kl$1@digitalmars.com...
>
> > BaseTypeTuple from std.traits seems to be the starting point, and recursively calling that template gets successive base types, but putting that together into a single template has defeated me. Anyone accomplished this?
>
> Here's a Tango-y version:
>
> import tango.core.Traits;
>
> template AllBasesImpl(T...)
> {
> static if(T.length == 0)
> alias Tuple!() AllBasesImpl;
> else
> alias Tuple!(T[0], AllBasesImpl!(BaseTypeTupleOf!(T[0])),
> AllBasesImpl!(T[1 .. $])) AllBasesImpl;
> }
>
> template AllBases(T)
> {
> alias Unique!(AllBasesImpl!(BaseTypeTupleOf!(T))) AllBases;
> }
>
> I believe the Phobos version would be something like this:
>
> import std.typetuple;
> import std.traits;
>
> template AllBasesImpl(T...)
> {
> static if(T.length == 0)
> alias TypeTuple!() AllBasesImpl;
> else
> alias TypeTuple!(T[0], AllBasesImpl!(BaseTypeTuple!(T[0])),
> AllBasesImpl!(T[1 .. $])) AllBasesImpl;
> }
>
> template AllBases(T)
> {
> alias NoDuplicates!(AllBasesImpl!(BaseTypeTuple!(T))) AllBases;
> }
>
>
Thanks Jarrett - works a treat.
|
Copyright © 1999-2021 by the D Language Foundation