summary refs log tree commit diff stats
path: root/tests/method/tmultim2.nim
blob: 75f6521371218eaa071578aa66699fa5bd183583 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
discard """
  file: "tmultim2.nim"
  output: "collide: unit, thing collide: unit, thing collide: thing, unit"
"""
# Test multi methods

type
  TThing = object {.inheritable.}
  TUnit = object of TThing
    x: int
  TParticle = object of TThing
    a, b: int
    
method collide(a, b: TThing) {.inline.} =
  quit "to override!"
  
method collide(a: TThing, b: TUnit) {.inline.} =
  write stdout, "collide: thing, unit "

method collide(a: TUnit, b: TThing) {.inline.} =
  write stdout, "collide: unit, thing "

proc test(a, b: TThing) {.inline.} =
  collide(a, b)

var
  a: TThing
  b, c: TUnit
collide(b, c) # ambiguous (unit, thing) or (thing, unit)? -> prefer unit, thing!
test(b, c)
collide(a, b)
#OUT collide: unit, thing collide: unit, thing collide: thing, unit