tmultim.nim 1.7 KB

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