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. static if (i == 0)
  22. return fun(args);
  23. else
  24. return f!(i - 1)(t.field[i - 1], args);
  25. }
  26. return f!(V.length)();
  27. }
  28.  
  29. /* main */
  30.  
  31. void main()
  32. {
  33. auto tup = tuple(
  34. 5, 3.14f, "hello world", [ 5, 10, 15 ], [ "foo":5, "bar":10 ]
  35. );
  36.  
  37. auto ret = unpack!foo(tup);
  38. writeln("retval from foo: ", ret);
  39. }
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