Hello everyone,
I'm playing with Nim for some months now and it's quite incredible!
I'm facing a little issue with POO and/or polymorphism concepts, bellow is an example of what I try to accomplish.
type
Animal* = ref object of RootObj
age*: int
type
Dog* = ref object of Animal
type
Cat* = ref object of Animal
proc test(self: Animal) =
echo "need to be overwritten"
proc test(self: Dog) =
echo "I'm a dog"
proc test(self: Cat) =
echo "I'm a cat"
when isMainModule:
var animals: seq[Animal]
animals.add(Dog())
animals.add(Cat())
for animal in items(animals):
animal.test()
I'm creating a new sequence which contains Animal objects. I add a Dog and a Cat objects which are a typeof Animal.
However, it seems I'm loosing their type, I'm getting the following output:
need to be overwritten
need to be overwritten
The output I wish:
I'm a dog
I'm a cat
If you have any tips to get it work, it would be awesome, thanks in advance :)
So you should be using {.base.} pragma
Taken from Mastering Nim" , Chapter 22:
" Methods are the only construct in Nim that uses dynamic dispatch, all other routines use static dispatch. Dynamic dispatch means that the runtime type of objects does influence with operation is performed".
https://play.nim-lang.org/#ix=41QY
" I didn't post this to be a smart-ass , cause I am learning to.
The problem is that you are using the proc keyword instead of the method keyword. The proc keyword is used for static dispatch, while the method keyword is used for dynamic dispatch. In this case, you want to use dynamic dispatch so that the correct method is called based on the runtime type of the object.
Proc A proc is a static method. This means that the compiler will choose which method to call at compile time, based on the static type of the object. In this case, the compiler will always choose the test method for objects of type Animal, even if they are actually of type Dog or Cat.
Method A method is a dynamic method. This means that the compiler will choose which method to call at runtime, based on the runtime type of the object. In this case, the compiler will call the test method for objects of type Dog, and the test method for objects of type Cat.
The problem is that you are using the proc keyword instead of the method keyword. The proc keyword is used for static dispatch, while the method keyword is used for dynamic dispatch. In this case, you want to use dynamic dispatch so that the correct method is called based on the runtime type of the object.
Proc A proc is a static method. This means that the compiler will choose which method to call at compile time, based on the static type of the object. In this case, the compiler will always choose the test method for objects of type Animal, even if they are actually of type Dog or Cat.
Method A method is a dynamic method. This means that the compiler will choose which method to call at runtime, based on the runtime type of the object. In this case, the compiler will call the test method for objects of type Dog, and the test method for objects of type Cat.