generic multi-methods should work now
This commit is contained in:
parent
34afaeeedd
commit
f5fa5dae85
9 changed files with 67 additions and 21 deletions
24
tests/method/tgeneric_methods.nim
Normal file
24
tests/method/tgeneric_methods.nim
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
discard """
|
||||
output: "wow2"
|
||||
"""
|
||||
type
|
||||
First[T] = ref object of RootObj
|
||||
value: T
|
||||
|
||||
Second[T] = ref object of First[T]
|
||||
value2: T
|
||||
|
||||
method wow[T](y: int; x: First[T]) {.base.} =
|
||||
echo "wow1"
|
||||
|
||||
method wow[T](y: int; x: Second[T]) =
|
||||
echo "wow2"
|
||||
|
||||
var
|
||||
x: Second[int]
|
||||
new(x)
|
||||
|
||||
proc takeFirst(x: First[int]) =
|
||||
wow(2, x)
|
||||
|
||||
takeFirst(x)
|
||||
|
|
@ -4,27 +4,27 @@ discard """
|
|||
# Test multi methods
|
||||
|
||||
type
|
||||
TThing = object {.inheritable.}
|
||||
TUnit[T] = object of TThing
|
||||
Thing = object {.inheritable.}
|
||||
Unit[T] = object of Thing
|
||||
x: T
|
||||
TParticle = object of TThing
|
||||
Particle = object of Thing
|
||||
a, b: int
|
||||
|
||||
method collide(a, b: TThing) {.base, inline.} =
|
||||
method collide(a, b: Thing) {.base, inline.} =
|
||||
quit "to override!"
|
||||
|
||||
method collide[T](a: TThing, b: TUnit[T]) {.inline.} =
|
||||
method collide[T](a: Thing, b: Unit[T]) {.inline.} =
|
||||
write stdout, "collide: thing, unit | "
|
||||
|
||||
method collide[T](a: TUnit[T], b: TThing) {.inline.} =
|
||||
method collide[T](a: Unit[T], b: Thing) {.inline.} =
|
||||
write stdout, "collide: unit, thing | "
|
||||
|
||||
proc test(a, b: TThing) {.inline.} =
|
||||
proc test(a, b: Thing) {.inline.} =
|
||||
collide(a, b)
|
||||
|
||||
var
|
||||
a: TThing
|
||||
b, c: TUnit[string]
|
||||
collide(b, TThing(c))
|
||||
a: Thing
|
||||
b, c: Unit[string]
|
||||
collide(b, Thing(c))
|
||||
test(b, c)
|
||||
collide(a, b)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue