tmultim.nim 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. discard """
  2. matrix: "--multimethods:on"
  3. output: '''
  4. collide: unit, thing
  5. collide: unit, thing
  6. collide: thing, unit
  7. collide: thing, thing
  8. collide: unit, thing |
  9. collide: unit, thing |
  10. collide: thing, unit |
  11. do nothing
  12. '''
  13. joinable: false
  14. disabled: true
  15. """
  16. # tmultim2
  17. type
  18. TThing {.inheritable.} = object
  19. TUnit = object of TThing
  20. x: int
  21. TParticle = object of TThing
  22. a, b: int
  23. method collide(a, b: TThing) {.base, inline.} =
  24. echo "collide: thing, thing"
  25. method collide(a: TThing, b: TUnit) {.inline.} =
  26. echo "collide: thing, unit"
  27. method collide(a: TUnit, b: TThing) {.inline.} =
  28. echo "collide: unit, thing"
  29. proc test(a, b: TThing) {.inline.} =
  30. collide(a, b)
  31. proc staticCollide(a, b: TThing) {.inline.} =
  32. procCall collide(a, b)
  33. var
  34. a: TThing
  35. b, c: TUnit
  36. collide(b, c) # ambiguous (unit, thing) or (thing, unit)? -> prefer unit, thing!
  37. test(b, c)
  38. collide(a, b)
  39. staticCollide(a, b)
  40. # tmultim6
  41. type
  42. Thing {.inheritable.} = object
  43. Unit[T] = object of Thing
  44. x: T
  45. Particle = object of Thing
  46. a, b: int
  47. method collide(a, b: Thing) {.base, inline.} =
  48. quit "to override!"
  49. method collide[T](a: Thing, b: Unit[T]) {.inline.} =
  50. echo "collide: thing, unit |"
  51. method collide[T](a: Unit[T], b: Thing) {.inline.} =
  52. echo "collide: unit, thing |"
  53. proc test(a, b: Thing) {.inline.} =
  54. collide(a, b)
  55. var
  56. aaa: Thing
  57. bbb, ccc: Unit[string]
  58. collide(bbb, Thing(ccc))
  59. test(bbb, ccc)
  60. collide(aaa, bbb)
  61. # tmethods1
  62. method somethin(obj: RootObj) {.base.} =
  63. echo "do nothing"
  64. type
  65. TNode* {.inheritable.} = object
  66. PNode* = ref TNode
  67. PNodeFoo* = ref object of TNode
  68. TSomethingElse = object
  69. PSomethingElse = ref TSomethingElse
  70. method foo(a: PNode, b: PSomethingElse) {.base.} = discard
  71. method foo(a: PNodeFoo, b: PSomethingElse) = discard
  72. var o: RootObj
  73. o.somethin()