alias tuples?

Jarrett Billingsley kb3ctd2 at yahoo.com
Sun Oct 14 20:21:34 PDT 2007


"Janice Caron" <caron800 at googlemail.com> wrote in message 
news:mailman.442.1192397473.16939.digitalmars-d at puremagic.com...

> OK, that's slightly more interesting. It looks like S.tupleof gets you
> type tuple, while s.tupleof gets you an expression tuple. But neither
> is what I'm looking for. I want to get _aliases_ into a tuple. This
> would open the door to powerful variadic metaprogramming, just as
> (non-variadic) alias template parameters do for (non-variadic)
> templates.
>
> Going back to structs, for example, member variable names are features
> of S (the structure declaration), not of s (an instance of S), so if
> we wanted to automate their extraction, you'd need a new property for
> struct types. But that would only really be icing on the cake, because
> really it's the aliasing side of things where it would matter most.
>
> See, right now, I have a quite nice piece of code which looks a little
> bit like this (I've simplified it for illustration purposes)
>
> struct A
> {
>    B b;
>    C c;
>
>    mixin SomeNiceFunctions!(b,c);
> }
>
> struct B
> {
>    D d;
>    E e;
>    F f;
>
>    mixin SomeNiceFunctions!(d,e,f);
> }
>
> struct C
> {
>    F f;
>    G g;
>    D d;
>    H h;
>
>    mixin SomeNiceFunctions!(f,g,d,h);
> }
>
> Now this is all very cool and marvellous, and already /way/ better
> than C++, but nonetheless I still have to write several different
> versions of the mixin, one for each different number of parameters.
> Specifically:
>
> template SomeNiceFunctions!(alias a,alias b) { /*...*/ }
> template SomeNiceFunctions!(alias a,alias b,alias c) { /*...*/ }
> template SomeNiceFunctions!(alias a,alias b,alias c,alias d) { /*...*/ }
>
> How much nicer it would be to instead be able to do:
>
> template SomeNiceFunctions!(alias a...) { /*...*/ }
>
> and then use a foreach (static foreach?) within the definition to
> dereference each alias in turn. That's really what I'm getting at.
>
> (Being able to automate the parameter list at the mixin site would be
> a nice added bonus, but not essential).

Maybe you didn't know this, but tuple parameters can be any mix of types, 
expressions, and aliases.  This is already entirely possible.

class X { void serialize() {} };
class Y : X{}
class Z : X{}

struct A
{
    X x;
    Y y;
    Z z;

    mixin SomeNiceFunctions!(x, y, z);
}

template SomeNiceFunctions(T...)
{
    static if(T.length > 0)
    {
        mixin(typeof(T[0]).stringof ~ " get" ~ T[0].stringof ~ "() { return 
" ~ T[0].stringof ~ "; }");

        static if(T.length > 1)
            mixin SomeNiceFunctions!(T[1 .. $]);
    }
}

void main()
{
    A a;
    Stdout.formatln("{}", a.getx() is null);
} 





More information about the Digitalmars-d mailing list