Destructured Tuple Assignment

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 15 06:40:00 PDT 2015


On Friday, 15 May 2015 at 12:22:55 UTC, Artur Skawina wrote:
>    template let(string D) {
>       mixin({
>          enum sdsl = D.findSplit("=");
>          mixin(`struct S { int `~sdsl[0]~`; }`);
>          string code = `auto v = ` ~ sdsl[2] ~ `;`;
>          foreach (I, _; typeof(S.tupleof))
>             code ~= `auto ` ~ S.tupleof[I].stringof ~ ` = 
> v[`~I.stringof~`]; `;
>          return code;
>       }());
>    }

I added support for auto, const and immutable declarations at

https://github.com/nordlow/justd/blob/master/ties.d#L96

which allows usage as

     mixin let!q{ auto i, d, s, c = tuple(42, 3.14, `pi`, 'x') };
     mixin let!q{ const i, d, s, c = tuple(42, 3.14, `pi`, 'x') };
     mixin let!q{ immutable i, d, s, c = tuple(42, 3.14, `pi`, 
'x') };

:)

See unittests for details


More information about the Digitalmars-d-learn mailing list