tmethod_issues.nim 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. discard """
  2. output: '''
  3. wof!
  4. wof!
  5. '''
  6. """
  7. # bug #1659
  8. type Animal = ref object {.inheritable.}
  9. type Dog = ref object of Animal
  10. method say(a: Animal): auto {.base.} = "wat!"
  11. method say(a: Dog): auto = "wof!"
  12. proc saySomething(a: Animal): auto = a.say()
  13. method ec(a: Animal): auto {.base.} = echo "wat!"
  14. method ec(a: Dog): auto = echo "wof!"
  15. proc ech(a: Animal): auto = a.ec()
  16. var a = Dog()
  17. echo saySomething(a)
  18. ech a
  19. # bug #2401
  20. type MyClass = ref object of RootObj
  21. method HelloWorld*(obj: MyClass) {.base.} =
  22. when defined(myPragma):
  23. echo("Hello World")
  24. # discard # with this line enabled it works
  25. var obj = MyClass()
  26. obj.HelloWorld()
  27. # bug #5432
  28. type
  29. Iterator[T] = ref object of RootObj
  30. # base methods with `T` in the return type are okay
  31. method methodThatWorks*[T](i: Iterator[T]): T {.base.} =
  32. discard
  33. # base methods without `T` (void or basic types) fail
  34. method methodThatFails*[T](i: Iterator[T]) {.base.} =
  35. discard
  36. type
  37. SpecificIterator1 = ref object of Iterator[string]
  38. SpecificIterator2 = ref object of Iterator[int]
  39. # bug #3431
  40. type
  41. Lexer = object
  42. buf*: string
  43. pos*: int
  44. lastchar*: char
  45. ASTNode = object
  46. method init*(self: var Lexer; buf: string) {.base.} =
  47. self.buf = buf
  48. self.pos = 0
  49. self.lastchar = self.buf[0]
  50. method init*(self: var ASTNode; val: string) =
  51. discard
  52. # bug #3370
  53. type
  54. RefTestA*[T] = ref object of RootObj
  55. data*: T
  56. method tester*[S](self: S): bool =
  57. true
  58. type
  59. RefTestB* = RefTestA[(string, int)]
  60. method tester*(self: RefTestB): bool =
  61. true
  62. type
  63. RefTestC = RefTestA[string]
  64. method tester*(self: RefTestC): bool =
  65. false
  66. # bug #3468
  67. type X = ref object of RootObj
  68. type Y = ref object of RootObj
  69. method draw*(x: X) {.base.} = discard
  70. method draw*(y: Y) {.base.} = discard
  71. # bug #3550
  72. type
  73. BaseClass = ref object of RootObj
  74. Class1 = ref object of BaseClass
  75. Class2 = ref object of BaseClass
  76. method test(obj: Class1, obj2: BaseClass) =
  77. discard
  78. method test(obj: Class2, obj2: BaseClass) =
  79. discard
  80. var obj1 = Class1()
  81. var obj2 = Class2()
  82. obj1.test(obj2)
  83. obj2.test(obj1)