tmultim.nim 1.8 KB

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