According to the Nim manual, the variable type is a "static type" while the actual value the variable points to in memory is the "dynamic type".
How is it possible they can be different types? I thought assigning the wrong type to a variable would be an error.
import typetraits
type
Person = ref object of RootObj
name*: string
age: int
Student = ref object of Person # a student is a person
id: int
method sayHi(p: Person) {.base.} =
echo "I'm a person"
method sayHi(s: Student) =
echo "I'm a student"
var student = Student(name: "Peter", age: 30, id: 10)
var person: Person = student # valid assignment to base type
echo person.repr # contains id as well
echo name(person.type) # static type = Person
person.sayHi() # dynamic type = I'm a student
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With