tinheritance.nim 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. discard """
  2. nimout: '''Hello fred, managed by sally
  3. Hello sally, managed by bob
  4. 0'''
  5. """
  6. # bug #3973
  7. type
  8. EmployeeCode = enum
  9. ecCode1,
  10. ecCode2
  11. Person* = object of RootObj
  12. name*: string
  13. last_name*: string
  14. Employee* = object of Person
  15. empl_code*: EmployeeCode
  16. mgr_name*: string
  17. proc test() =
  18. var
  19. empl1 = Employee(name: "fred", last_name: "smith", mgr_name: "sally", empl_code: ecCode1)
  20. empl2 = Employee(name: "sally", last_name: "jones", mgr_name: "bob", empl_code: ecCode2)
  21. echo "Hello ", empl1.name, ", managed by ", empl1.mgr_name
  22. echo "Hello ", empl2.name, ", managed by ", empl2.mgr_name
  23. static:
  24. test()
  25. #----------------------------------------------
  26. # Bugs #9701 and #9702
  27. type
  28. MyKind = enum
  29. kA, kB, kC
  30. Base = ref object of RootObj
  31. x: string
  32. A = ref object of Base
  33. a: string
  34. B = ref object of Base
  35. b: string
  36. C = ref object of B
  37. c: string
  38. template check_templ(n: Base, k: MyKind) =
  39. if k == kA: doAssert(n of A) else: doAssert(not (n of A))
  40. if k in {kB, kC}: doAssert(n of B) else: doAssert(not (n of B))
  41. if k == kC: doAssert(n of C) else: doAssert(not (n of C))
  42. doAssert(n of Base)
  43. proc check_proc(n: Base, k: MyKind) =
  44. if k == kA: doAssert(n of A) else: doAssert(not (n of A))
  45. if k in {kB, kC}: doAssert(n of B) else: doAssert(not (n of B))
  46. if k == kC: doAssert(n of C) else: doAssert(not (n of C))
  47. doAssert(n of Base)
  48. static:
  49. let aa = new(A)
  50. check_templ(aa, kA)
  51. check_proc(aa, kA)
  52. let bb = new(B)
  53. check_templ(bb, kB)
  54. check_proc(bb, kB)
  55. let cc = new(C)
  56. check_templ(cc, kC)
  57. check_proc(cc, kC)
  58. let aa = new(A)
  59. check_templ(aa, kA)
  60. check_proc(aa, kA)
  61. let bb = new(B)
  62. check_templ(bb, kB)
  63. check_proc(bb, kB)
  64. let cc = new(C)
  65. check_templ(cc, kC)
  66. check_proc(cc, kC)
  67. type
  68. BBar = object of RootObj
  69. bbarField: set[char]
  70. xbarField: string
  71. d, e: int
  72. FooBar = object of BBar
  73. a: int
  74. b: string
  75. static:
  76. var fb: FooBar
  77. echo fb.a