tselect.nim 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. discard """
  2. output: '''abcsuffix
  3. xyzsuffix
  4. destroy foo 2
  5. destroy foo 1
  6. '''
  7. cmd: '''nim c --gc:arc $file'''
  8. """
  9. proc select(cond: bool; a, b: sink string): string =
  10. if cond:
  11. result = a # moves a into result
  12. else:
  13. result = b # moves b into result
  14. proc test(param: string; cond: bool) =
  15. var x = "abc" & param
  16. var y = "xyz" & param
  17. # possible self-assignment:
  18. x = select(cond, x, y)
  19. echo x
  20. # 'select' must communicate what parameter has been
  21. # consumed. We cannot simply generate:
  22. # (select(...); wasMoved(x); wasMoved(y))
  23. test("suffix", true)
  24. test("suffix", false)
  25. #--------------------------------------------------------------------
  26. # issue #13659
  27. type
  28. Foo = ref object
  29. data: int
  30. parent: Foo
  31. proc `=destroy`(self: var type(Foo()[])) =
  32. echo "destroy foo ", self.data
  33. for i in self.fields: i.reset
  34. proc getParent(self: Foo): Foo = self.parent
  35. var foo1 = Foo(data: 1)
  36. var foo2 = Foo(data: 2, parent: foo1)
  37. foo2.getParent.data = 1