tobject.nim 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. type Obj = object
  2. foo: int
  3. proc makeObj(x: int): Obj =
  4. result.foo = x
  5. block: # object basic methods
  6. block: # it should convert an object to a string
  7. var obj = makeObj(1)
  8. # Should be "obj: (foo: 1)" or similar.
  9. doAssert($obj == "(foo: 1)")
  10. block: # it should test equality based on fields
  11. doAssert(makeObj(1) == makeObj(1))
  12. # bug #10203
  13. type
  14. TMyObj = TYourObj
  15. TYourObj = object of RootObj
  16. x, y: int
  17. proc init: TYourObj =
  18. result.x = 0
  19. result.y = -1
  20. proc f(x: var TYourObj) =
  21. discard
  22. var m: TMyObj = init()
  23. f(m)
  24. var a: TYourObj = m
  25. var b: TMyObj = a
  26. # bug #10195
  27. type
  28. InheritableFoo {.inheritable.} = ref object
  29. InheritableBar = ref object of InheritableFoo # ERROR.
  30. block: # bug #14698
  31. const N = 3
  32. type Foo[T] = ref object
  33. x1: int
  34. when N == 2:
  35. x2: float
  36. when N == 3:
  37. x3: seq[int]
  38. else:
  39. x4: char
  40. x4b: array[9, char]
  41. let t = Foo[float](x1: 1)
  42. doAssert $(t[]) == "(x1: 1, x3: @[])"
  43. doAssert t.sizeof == int.sizeof
  44. type Foo1 = object
  45. x1: int
  46. x3: seq[int]
  47. doAssert t[].sizeof == Foo1.sizeof
  48. # bug #147
  49. type
  50. TValue* {.pure, final.} = object of RootObj
  51. a: int
  52. PValue = ref TValue
  53. PPValue = ptr PValue
  54. var x: PValue
  55. new x
  56. var sp: PPValue = addr x
  57. sp.a = 2
  58. doAssert sp.a == 2