fork download
  1. import std.stdio, std.typecons;
  2.  
  3. struct My_Tuple(T...)
  4. {
  5. Tuple!T base;
  6. alias base this;
  7.  
  8. this(T args) { this.base(args); }
  9. U opIndex(U)(size_t idx) { return this.field[idx]; }
  10. }
  11.  
  12. My_Tuple!T make_tuple(T...)(T args)
  13. {
  14. return typeof(return)(args);
  15. }
  16.  
  17. /* TESTING FUNCTION */
  18.  
  19. int[] foo(T...)(T args)
  20. {
  21. writeln("Called foo ...");
  22. foreach (i, arg; args)
  23. writefln(" args[%d] == %s", i, arg);
  24. writeln("... foo end");
  25.  
  26. return [ 5, 10, 15, 20 ];
  27. }
  28.  
  29. /* UNPACKER */
  30.  
  31. auto unpack(alias fun, V...)(ref My_Tuple!V t)
  32. {
  33. auto f(size_t i, U...)(U args)
  34. {
  35. static if (i == 0)
  36. return fun(args);
  37. else
  38. return f!(i - 1)(t.field[i - 1], args);
  39. }
  40. return f!(V.length)();
  41. }
  42.  
  43. /* main */
  44.  
  45. void main()
  46. {
  47. auto tup = make_tuple(
  48. 5, 3.14f, "hello world", [ 5, 10, 15 ], [ "foo":5, "bar":10 ]
  49. );
  50.  
  51. auto ret = unpack!foo(tup);
  52. writeln("retval from foo: ", ret);
  53. }
  54.  
Success #stdin #stdout 0.01s 2152KB
stdin
Standard input is empty
stdout
Called foo ...
    args[0] == 0
    args[1] == nan
    args[2] == 
    args[3] == 
    args[4] == []
... foo end
retval from foo: 5 10 15 20