tobjects_various.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. discard """
  2. output: '''
  3. 34
  4. b
  5. wohoo
  6. baz
  7. '''
  8. """
  9. block tobject2:
  10. # Tests the object implementation
  11. type
  12. TPoint2d {.inheritable.} = object
  13. x, y: int
  14. TPoint3d = object of TPoint2d
  15. z: int # added a field
  16. proc getPoint( p: var TPoint2d) =
  17. {.breakpoint.}
  18. writeLine(stdout, p.x)
  19. var p: TPoint3d
  20. TPoint2d(p).x = 34
  21. p.y = 98
  22. p.z = 343
  23. getPoint(p)
  24. block tofopr:
  25. type
  26. TMyType = object {.inheritable.}
  27. len: int
  28. data: string
  29. TOtherType = object of TMyType
  30. proc p(x: TMyType): bool =
  31. return x of TOtherType
  32. var
  33. m: TMyType
  34. n: TOtherType
  35. doAssert p(m) == false
  36. doAssert p(n)
  37. block toop:
  38. type
  39. TA = object of RootObj
  40. x, y: int
  41. TB = object of TA
  42. z: int
  43. TC = object of TB
  44. whatever: string
  45. proc p(a: var TA) = echo "a"
  46. proc p(b: var TB) = echo "b"
  47. var c: TC
  48. p(c)
  49. block tfefobjsyntax:
  50. type
  51. Foo = object
  52. a, b: int
  53. s: string
  54. FooBar = object of RootObj
  55. n, m: string
  56. Baz = object of FooBar
  57. proc invoke(a: ref Baz) =
  58. echo "baz"
  59. # check object construction:
  60. let x = (ref Foo)(a: 0, b: 45, s: "wohoo")
  61. echo x.s
  62. var y: ref FooBar = (ref Baz)(n: "n", m: "m")
  63. invoke((ref Baz)(y))