Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. // rdmd -unittest -main -dip25 dip25.d
  2.  
  3. private struct Container
  4. {
  5. this(int c) @safe
  6. {
  7. arr = new int[](c);
  8. arr[] = c;
  9. }
  10.  
  11. ~this() @safe
  12. {
  13. arr[] = -1;
  14. }
  15.  
  16. ref Range opSlice() return @safe // remove "return" and this code correctly fails to compile
  17. {
  18. r.index = 0;
  19. r.c = &this;
  20. return r;
  21. }
  22.  
  23. @safe struct Range
  24. {
  25. int front() { return c.arr[index]; }
  26. bool empty() { return index >= c.arr.length; }
  27. void popFront() { index++; }
  28. size_t index;
  29. Container* c;
  30. }
  31.  
  32. private:
  33. Range r;
  34. int[] arr;
  35. }
  36.  
  37. private struct S
  38. {
  39. void takesContainer(ref Container c) @safe
  40. {
  41. this.r = c[];
  42. }
  43.  
  44. void print() @safe
  45. {
  46. import std.stdio:writeln;
  47. writeln(r);
  48. }
  49.  
  50. Container.Range r;
  51. }
  52.  
  53. void doStuff(ref S s) @safe
  54. {
  55. auto c = Container(20);
  56. s.takesContainer(c);
  57. }
  58.  
  59. @safe unittest
  60. {
  61. S s;
  62. doStuff(s);
  63. s.print();
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement