tobjects_various.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. writeLine(stdout, p.x)
  18. var p: TPoint3d
  19. TPoint2d(p).x = 34
  20. p.y = 98
  21. p.z = 343
  22. getPoint(p)
  23. block tofopr:
  24. type
  25. TMyType = object {.inheritable.}
  26. len: int
  27. data: string
  28. TOtherType = object of TMyType
  29. proc p(x: TMyType): bool =
  30. return x of TOtherType
  31. var
  32. m: TMyType
  33. n: TOtherType
  34. doAssert p(m) == false
  35. doAssert p(n)
  36. block toop:
  37. type
  38. TA = object of RootObj
  39. x, y: int
  40. TB = object of TA
  41. z: int
  42. TC = object of TB
  43. whatever: string
  44. proc p(a: var TA) = echo "a"
  45. proc p(b: var TB) = echo "b"
  46. var c: TC
  47. p(c)
  48. block tfefobjsyntax:
  49. type
  50. Foo = object
  51. a, b: int
  52. s: string
  53. FooBar = object of RootObj
  54. n, m: string
  55. Baz = object of FooBar
  56. proc invoke(a: ref Baz) =
  57. echo "baz"
  58. # check object construction:
  59. let x = (ref Foo)(a: 0, b: 45, s: "wohoo")
  60. echo x.s
  61. var y: ref FooBar = (ref Baz)(n: "n", m: "m")
  62. invoke((ref Baz)(y))
  63. block t3012:
  64. type
  65. A {.inheritable.} = object
  66. C {.inheritable.} = ref object
  67. type
  68. AA = ref object of A
  69. CC = ref object of C
  70. block t7244:
  71. type
  72. Foo = ref object of RootRef
  73. Bar = ref object of Foo
  74. proc test(foo: var Foo) = discard
  75. proc test(bar: var Bar) = test(Foo(bar))