Here is your example translated to Nim using a method for "cry".
type
Animal* = ref object of RootObj
weightOfAnimal*: int
method cry(x: Animal) {.base.} =
echo "Animal is crying..."
type Dog* = ref object of Animal
name*: string
proc newDog*(sName: string = ""): Dog =
new(result)
result.name = sName
method cry*(x: Dog) =
procCall x.Animal.cry()
echo "But Dog is barking..."
let myAnimal1 = newDog()
let myAnimal2 = Dog(name: "Médor")
myAnimal1.cry
myAnimal2.cry
Run
Some explanations. I have used ref objects but could have used object instead.
The only reason here is for convenience as I can use a proc "newDog" to create
Dog objects.
There is no use for a proc "newAnimal" as the standard way to create Animal
objects is sufficient (weight is initialized at 0 anyway). There is no real
need for a proc "newDog" here but I have kept it for demonstration purpose.
To call the Animal "cry", you need to use procCall to bypass the dynamic
binding. And you need a conversion to Animal of course. This way, you have full
control over what you want to call in the class hierarchy.
To create a Dog object, we can use the "newDog" proc or we can simply provide
the values of the fields to initialize.
For the name "Médor" for the dog, this is supposed to be a common dog name in
French (never encountered a dog with this name though :-)). I don’t know the
common dog name in English.
Hope this helps.