Multi-methods

Note: Starting from Nim 0.20, to use multi-methods one must explicitly pass --multimethods:on when compiling.

In a multi-method all parameters that have an object type are used for the dispatching:

  1. type
  2. Thing = ref object of RootObj
  3. Unit = ref object of Thing
  4. x: int
  5. method collide(a, b: Thing) {.inline.} =
  6. quit "to override!"
  7. method collide(a: Thing, b: Unit) {.inline.} =
  8. echo "1"
  9. method collide(a: Unit, b: Thing) {.inline.} =
  10. echo "2"
  11. var a, b: Unit
  12. new a
  13. new b
  14. collide(a, b) # output: 2