Destructured Tuple Assignment

Artur Skawina via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 15 05:22:38 PDT 2015


   import std.algorithm;

   template magicassign(A...) {
      void magicassign(B)(B b) @property {
         foreach(I, ref a; A)
            static if (!is(typeof(A[I]):typeof(null)))
               a = b[I];
      }
   }

   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;
      }());
   }

   void main(string[] args) {
      import std.stdio;

      string a, b;
      magicassign!(a, null, b) = args[1].findSplit("-");
      writeln(a);
      writeln(b);

      mixin let!q{ c, _, d = args[1].findSplit("-") };
      writeln(c);
      writeln(d);
   }

artur


More information about the Digitalmars-d-learn mailing list