alias tuples?

Janice Caron caron800 at googlemail.com
Sun Oct 14 14:31:06 PDT 2007


On 10/14/07, Manuel König <ManuelK89 at gmx.net> wrote:
> looking at http://www.digitalmars.com/d/tuple.html it says:
>
> "The data fields of a struct or class can be turned into an _expression_
> tuple using the .tupleof  property"
> (below that you find an example that actually uses it)
>
> I tested it myself and it works.

You mean this...?

# struct S { int x; long y; }
#
# void foo(int a, long b)
# {
#     writefln(a, b);
# }
#
# ...
# S s;
# s.x = 7;
# s.y = 8;
# foo(s.x, s.y);	// prints 78
# foo(s.tupleof);	// prints 78
# s.tupleof[1] = 9;
# s.tupleof[0] = 10;
# foo(s.tupleof); // prints 109
# s.tupleof[2] = 11;  // error, no third field of S

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).




More information about the Digitalmars-d mailing list