fork download
  1. import std.stdio, std.typecons;
  2.  
  3. int[] foo(T...)(T args)
  4. {
  5. writeln("Called foo ...");
  6. foreach (i, arg; args)
  7. writefln(" args[%d] == %s", i, arg);
  8. writeln("... foo end");
  9.  
  10. return [ 5, 10, 15, 20 ];
  11. }
  12.  
  13. auto unpack(alias fun, V...)(ref Tuple!V t)
  14. {
  15. auto f(size_t i, U...)(U args)
  16. {
  17. static if (i == 0)
  18. return fun(args);
  19. else
  20. return f!(i - 1)(t.field[i - 1], args);
  21. }
  22. return f!(V.length)();
  23. }
  24.  
  25. void main()
  26. {
  27. auto tup = tuple(
  28. 5, 3.14f, "hello world", [ 5, 10, 15 ], [ "foo":5, "bar":10 ]
  29. );
  30.  
  31. auto ret = unpack!foo(tup);
  32. writeln("retval from foo: ", ret);
  33. }
  34.  
Success #stdin #stdout 0.02s 2152KB
stdin
Standard input is empty
stdout
Called foo ...
    args[0] == 5
    args[1] == 3.14
    args[2] == hello world
    args[3] == 5 10 15
    args[4] == [bar:10,foo:5]
... foo end
retval from foo: 5 10 15 20