Hi
I have had hard time with method which did not behave as I expected to.
child.nim
type Parent* = ref object of RootObj
type Child* = ref object of Parent
type Some = object
method getDataToSave*(x: Parent): object {.base.} =
echo("took getDataToSave Parent")
var o: Some
return o
method getDataToSave*(x: Child): object =
echo("took getDataToSave from Child")
var o: Some
return o
main.nim
from child import Parent, Child, getDataToSave
proc test() =
var x: Parent = Child()
discard getDataToSave(x)
test()
It compiles and gives "took getDataToSave from Parent", whilst I expected method on Child would be invoked. After long long time I noticed that problem lies in return type which is object. If you change child.nim
type Parent* = ref object of RootObj
type Child* = ref object of Parent
type Some = object
method getDataToSave*(x: Parent): Some {.base.} =
echo("took getDataToSave Parent")
var o: Some
return o
method getDataToSave*(x: Child): Some =
echo("took getDataToSave from Child")
var o: Some
return o
It is working as expected, which means I get "took getDataToSave from Child". Perhaps compiler should give warning?