teffects12.nim 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. discard """
  2. action: compile
  3. """
  4. import std/locks
  5. type
  6. Test2Effect* = object
  7. Test2* = object
  8. value2*: int
  9. Test1Effect* = object
  10. Test1* = object
  11. value1*: int
  12. Main* = object
  13. test1Lock: Lock
  14. test1: Test1
  15. test2Lock: Lock
  16. test2: Test2
  17. proc `=copy`(obj1: var Test2, obj2: Test2) {.error.}
  18. proc `=copy`(obj1: var Test1, obj2: Test1) {.error.}
  19. proc `=copy`(obj1: var Main, obj2: Main) {.error.}
  20. proc withTest1(main: var Main,
  21. fn: proc(test1: var Test1) {.gcsafe, forbids: [Test1Effect].}) {.gcsafe, tags: [Test1Effect, RootEffect].} =
  22. withLock(main.test1Lock):
  23. fn(main.test1)
  24. proc withTest2(main: var Main,
  25. fn: proc(test1: var Test2) {.gcsafe, forbids: [Test2Effect].}) {.gcsafe, tags: [Test2Effect, RootEffect].} =
  26. withLock(main.test2Lock):
  27. fn(main.test2)
  28. proc newMain(): Main =
  29. var test1lock: Lock
  30. initLock(test1Lock)
  31. var test2lock: Lock
  32. initLock(test2Lock)
  33. var main = Main(test1Lock: move(test1Lock), test1: Test1(value1: 1),
  34. test2Lock: move(test2Lock), test2: Test2(value2: 2))
  35. main.withTest1(proc(test1: var Test1) = test1.value1 += 1)
  36. main.withTest2(proc(test2: var Test2) = test2.value2 += 1)
  37. move main
  38. var main = newMain()
  39. main.withTest1(proc(test1: var Test1) =
  40. test1.value1 += 1
  41. main.withTest2(proc(test2: var Test2) = test2.value2 += 1)
  42. )
  43. main.withTest1(proc(test1: var Test1) {.tags: [].} = echo $test1.value1)
  44. main.withTest2(proc(test2: var Test2) {.tags: [].} = echo $test2.value2)