fork download
  1. import std.stdio, std.typecons;
  2.  
  3. /* TESTING FUNCTION */
  4.  
  5. int[] foo(T...)(T args)
  6. {
  7. writeln("Called foo ...");
  8. foreach (i, arg; args)
  9. writefln(" args[%d] == %s", i, arg);
  10. writeln("... foo end");
  11.  
  12. return [ 5, 10, 15, 20 ];
  13. }
  14.  
  15. /* UNPACKER */
  16.  
  17. auto unpack(alias fun, V...)(ref Tuple!V t)
  18. {
  19. auto f(size_t i, U...)(U args)
  20. {
  21. /* 2.043 and newer - t[i - 1] would work just fine */
  22. static if (i == 0)
  23. return fun(args);
  24. else
  25. return f!(i - 1)(t.field[i - 1], args);
  26. }
  27. return f!(V.length)();
  28. }
  29.  
  30. /* main */
  31.  
  32. void main()
  33. {
  34. auto tup = tuple(
  35. 5, 3.14f, "hello world", [ 5, 10, 15 ], [ "foo":5, "bar":10 ]
  36. );
  37.  
  38. auto ret = unpack!foo(tup);
  39. writeln("retval from foo: ", ret);
  40. }
Success #stdin #stdout 0.01s 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