Filling a Tuple-like struct in variadic member

Philippe Sigaud philippe.sigaud at gmail.com
Tue Nov 8 03:55:36 PST 2011


On Tue, Nov 8, 2011 at 12:21, RivenTheMage <riven-mage at id.ru> wrote:
> Is it possible to do such thing without using string mixin?

Hi,

you can use another tuple:


struct Key(T...)
{
    T fields;
    uint numFields = T.length;

    void assignFields(U...)(U args) if (U.length == T.length)
    {
        foreach(i, field; fields) fields[i] = args[i];
    }
}

void main()
{
    Key!(string, int, int delegate(int)) testval;

    writeln("Numfields: ", testval.numFields);

    testval.assignFields("abc", 1, (int i) {return i+1;});
    writeln(testval);
}

You can also test at compile-time if U and T are compatible, before
the assignement.
Or do you really need assignField to work with runtime values ?


Philippe


More information about the Digitalmars-d-learn mailing list