tphantomgeneric2.nim 801 B

12345678910111213141516171819202122232425262728293031323334
  1. discard """
  2. output: '''
  3. created Phantom[system.float] with value 1
  4. created Phantom[system.string] with value 2
  5. created Phantom[system.byte] with value 3
  6. destroyed Phantom[system.byte] with value 3
  7. destroyed Phantom[system.string] with value 2
  8. destroyed Phantom[system.float] with value 1
  9. '''
  10. """
  11. # issue #24374
  12. type Phantom[T] = object
  13. value: int
  14. # markerField: T
  15. proc initPhantom[T](value: int): Phantom[T] =
  16. doAssert value >= 0
  17. echo "created " & $Phantom[T] & " with value " & $value
  18. result = Phantom[T](value: value)
  19. proc `=wasMoved`[T](x: var Phantom[T]) =
  20. x.value = -1
  21. proc `=destroy`[T](x: Phantom[T]) =
  22. if x.value >= 0:
  23. echo "destroyed " & $Phantom[T] & " with value " & $x.value
  24. let
  25. x = initPhantom[float](1)
  26. y = initPhantom[string](2)
  27. z = initPhantom[byte](3)