Destructured Tuple Assignment

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 15 04:48:52 PDT 2015


On Friday, 15 May 2015 at 11:04:24 UTC, Per Nordlöw wrote:
> I'm guessing the closest thing we can get in current D version 
> is something like
>
>     let(q{first, _, second},
>         expression.findSplit(separator));
>
> right?

Correction: I believe it must look like

      let!q{first, _, second}(expression.findSplit(separator));

or

      let!`first, _, second`(expression.findSplit(separator));


Is it possible to define a let that does what I want here?

If so, could someone, pleeeze, help me write out a stub for this?

I'm guessing something along the lines of

mixin template let(string vars, Ts...)
{
     import std.range: splitter;

     // declare variables in current scope. TODO do we need a 
mixin here?
     foreach (i, var; vars.splitter(`, `))
     {
         mixin(Ts[i] ~ ` ` ~ var);
     }

     auto let(Tuple!Ts xs)
     {
         foreach (i, var; vars.splitter(`, `))
         {
             mixin(Ts[i] ~ ` = ` ~ xs[i]);
         }
     }
}

unittest
{
     let!q{first, _, second}(tuple(`first`, `_`, `second`));
}

but this fails in many ways.

Could someone, please help out here?


More information about the Digitalmars-d-learn mailing list