tfinalizer.nim 595 B

1234567891011121314151617181920212223242526272829303132
  1. discard """
  2. cmd: "nim c --gc:arc $file"
  3. output: '''Foo(field: "Dick Laurent", k: ka, x: 0.0)
  4. Nobody is dead
  5. Dick Laurent is dead'''
  6. """
  7. type
  8. Kind = enum
  9. ka, kb
  10. Foo = ref object
  11. field: string
  12. case k: Kind
  13. of ka: x: float
  14. of kb: discard
  15. #var x = Foo(field: "lovely")
  16. proc finalizer(x: Foo) =
  17. echo x.field, " is dead"
  18. var x: Foo
  19. new(x, finalizer)
  20. x.field = "Dick Laurent"
  21. # reference to a great movie. If you haven't seen it, highly recommended.
  22. echo repr x
  23. # bug #13112: bind the same finalizer multiple times:
  24. var xx: Foo
  25. new(xx, finalizer)
  26. xx.field = "Nobody"