tobjecttyperel.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. discard """
  2. output: '''(peel: 0, color: 15)
  3. (color: 15)
  4. 17
  5. (width: 0.0, taste: "", color: 13)
  6. (width: 0.0, taste: "", color: 15)
  7. cool
  8. test'''
  9. """
  10. # bug #5241
  11. type
  12. BaseFruit[T] = object of RootObj
  13. color: T
  14. MidLevel[T] = object of BaseFruit[T]
  15. Mango = object of MidLevel[int]
  16. peel: int
  17. Peach[X, T, Y] = object of T
  18. width: X
  19. taste: Y
  20. proc setColor[T](self: var BaseFruit[T]) =
  21. self.color = 15
  22. proc setColor[T](self: var BaseFruit[T], c: int) =
  23. self.color = c
  24. var c: Mango
  25. setColor(c)
  26. echo c
  27. var d: MidLevel[int]
  28. setColor(d)
  29. echo d
  30. type
  31. FooBase[T] = ref object of RootRef
  32. v: T
  33. BarClient = ref object of FooBase[int]
  34. proc getColor[T](f: FooBase[T]): T = 17
  35. var b: BarClient
  36. echo getColor(b)
  37. var z: Peach[float64, BaseFruit[int], string]
  38. z.setColor(13)
  39. echo z
  40. z.setColor()
  41. echo z
  42. # bug #5411
  43. type
  44. Foo[T] = ref object of RootRef
  45. v: T
  46. Bar = ref object of Foo[int]
  47. method m(o: RootRef) {.base.} = assert(false, "Abstract method called")
  48. method m[T](o: Foo[T]) = echo "cool"
  49. var v: Bar
  50. v.new()
  51. v.m() # Abstract method not called anymore
  52. # bug #88
  53. type
  54. TGen[T] = object of RootObj
  55. field: T
  56. TDerived[T] = object of TGen[T]
  57. nextField: T
  58. proc doSomething[T](x: ref TGen[T]) =
  59. type
  60. Ty = ref TDerived[T]
  61. echo Ty(x).nextField
  62. var
  63. x: ref TDerived[string]
  64. new(x)
  65. x.nextField = "test"
  66. doSomething(x)