fork download
  1. import std.stdio;
  2. class Foo {
  3. int a;
  4. int opApply(scope int delegate(ref int) dg)const
  5. {
  6. auto r = dg(a); // なんで渡せるの?
  7. if (r) return r;
  8. return 0;
  9. }
  10. }
  11. class Bar {
  12. Foo foo;
  13. this(){ foo = new Foo; }
  14. void hoge() const
  15. {
  16. writeln(foo.a);
  17. // なんでforeachbodyがconstじゃないdelegateで渡せちゃうの?
  18. foreach(ref fuga; foo){
  19. fuga = 1;
  20. }
  21. // なんで書き換えられちゃってるの?
  22. writeln(foo.a);
  23. }
  24. }
  25. void main() {
  26. (new Bar()).hoge();
  27. }
Success #stdin #stdout 0.01s 2120KB
stdin
Standard input is empty
stdout
0
1