fork download
  1. import std.stdio, std.typecons;
  2.  
  3. /* hack for old ass ideone compiler
  4.  * compilers prior to 2.042 (Feb 2010)
  5.  */
  6. static if (__VERSION__ <= 2041)
  7. {
  8. struct My_Tuple(T...)
  9. {
  10. Tuple!T base;
  11. alias base this;
  12.  
  13. this(T args) { this.base(args); }
  14. U opIndex(U)(size_t idx) { return this.field[idx]; }
  15. }
  16. }
  17. else /* 2.043 and newer */
  18. {
  19. alias Tuple My_Tuple;
  20. }
  21.  
  22. My_Tuple!T make_tuple(T...)(T args)
  23. {
  24. return typeof(return)(args);
  25. }
  26.  
  27. /* TESTING FUNCTION */
  28.  
  29. int[] foo(T...)(T args)
  30. {
  31. writeln("Called foo ...");
  32. foreach (i, arg; args)
  33. writefln(" args[%d] == %s", i, arg);
  34. writeln("... foo end");
  35.  
  36. return [ 5, 10, 15, 20 ];
  37. }
  38.  
  39. /* UNPACKER */
  40.  
  41. auto unpack(alias fun, V...)(ref My_Tuple!V t)
  42. {
  43. auto f(size_t i, U...)(U args)
  44. {
  45. static if (i == 0)
  46. return fun(args);
  47. else
  48. return f!(i - 1)(t.field[i - 1], args);
  49. }
  50. return f!(V.length)();
  51. }
  52.  
  53. /* main */
  54.  
  55. void main()
  56. {
  57. auto tup = make_tuple(
  58. 5, 3.14f, "hello world", [ 5, 10, 15 ], [ "foo":5, "bar":10 ]
  59. );
  60.  
  61. auto ret = unpack!foo(tup);
  62. writeln("retval from foo: ", ret);
  63. }
  64.  
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