On 08/27/2019 10:17 PM, Jabari Zakiya wrote:

> I can't do (a, b, c,d) = func1(i) directly.
> What do I do to assign the output of func1 to the individual variables?

I had some time to play with the following syntax, similar usage of which has been proposed a number of times as a replacement for tuple expansion. Assuming that foo() returns a struct of int, double, string; the following expression will set the variables to those members:

  int i;
  double d;
  string s;

  foo(42).into!(i, d, s);

Here is the complete program:

import std.stdio;
import std.string;

struct S {
  int i;
  double d;
  string s;
}

S foo(int i) {
  return S(i, 1.5, "hi");
}

template into(args...) {
  auto into(From)(From from)
  if (is (From == struct))
  {
    static foreach (i, m; __traits(allMembers, From)) {{
      alias argT = typeof(args[i]);
      alias memT = typeof(__traits(getMember, from, m));
      static assert (is (argT == memT),
format!"Cannot expand '%s %s.%s' into '%s %s' argument." (memT.stringof, From.stringof, m, argT.stringof, args[i].stringof));
      mixin (format!"args[%s] = from.%s;"(i, m));
    }}
  }
}

void main() {
  int i;
  double d;
  string s;

  foo(42).into!(i, d, s);

  writeln(i);
  writeln(d);
  writeln(s);
}

I know that it does not address attributes like shared, etc. but it shows how expressive nested templates can be.

Ali

Reply via email to