alias tuples?
Jarrett Billingsley
kb3ctd2 at yahoo.com
Sun Oct 14 09:23:03 PDT 2007
"Janice Caron" <caron800 at googlemail.com> wrote in message
news:mailman.422.1192341718.16939.digitalmars-d at puremagic.com...
> We've got value tuples, and we've got type tuples. Could we also have
> alias tuples?
>
> Here's what I mean. Right now I can do:
>
> struct A
> {
> X x;
> Y y;
> Z z;
>
> mixin Serialize!(x,y,z);
> }
> A a;
>
> template Serialize(alias x,alias y,alias z)
> {
> void serialize()
> {
> x.serialize();
> y.serialize();
> z.serialize();
> }
> }
>
> where the types X, Y and Z all have a serialize() function (and so on
> recursively for every object in need of serialization). Now it seems
> to me, if I wanted to make Serialize!() variadic, I'd need not a value
> tuple, nor a type tuple, but an "alias tuple". I'd want to be able to
> write something like:
>
> struct A
> {
> X x;
> Y y;
> Z z;
>
> mixin Serialize!(this.aliastupleof); /* expands to (x,y,z) */
> }
> A a;
>
> template Serialize(alias T...)
> {
> void serialize()
> {
> foreach(a;T)
> a.serialize();
> }
> }
>
> Of course this won't work right now. The template won't compile
> because (alias T...) has no meaning, and even if it did, structs don't
> have an aliastupleof property.
>
> Thoughts?
In fact you can do this all without tuples, at least as parameters to
templates.
class X { void serialize() {} }
class Y : X{}
class Z : X{}
struct A
{
X x;
Y y;
Z z;
mixin Serialize;
}
template Serialize()
{
void serialize()
{
foreach(a; this.tupleof)
a.serialize();
}
}
More information about the Digitalmars-d
mailing list