fork download
  1. import std.stdio;
  2.  
  3. class Foo
  4. {
  5. void xyz() { writeln("Xyz without arguments exists!"); }
  6.  
  7. void opDispatch(string name, T...)(T args)
  8. in
  9. {
  10. static assert (name == "foo" || name == "baz", "Invalid method called!");
  11. }
  12. body
  13. {
  14. writeln("Called method: ", name);
  15. foreach (i, arg; args) writefln("args[%d] == %s", i, arg);
  16. }
  17. }
  18.  
  19. void main()
  20. {
  21. Foo bar = new Foo();
  22. bar.baz();
  23. bar.xyz();
  24. bar.foo("hello", [5,10,15], 3.14f, ["foo":5, "bar":10], true);
  25. }
Success #stdin #stdout 0.01s 2148KB
stdin
Standard input is empty
stdout
Called method: baz
Xyz without arguments exists!
Called method: foo
args[0] == hello
args[1] == 5 10 15
args[2] == 3.14
args[3] == [bar:10,foo:5]
args[4] == true