How can there be a "static type" and a "dynamic type"?

According to Nim's guide, the type of a variable is a “static type”, while the actual value indicated by the variable in memory is a “dynamic type”.

How is this possible, they can be of different types? I thought assigning the wrong type to a variable would be a mistake.

+4
source share
1 answer
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
+7
source

Source: https://habr.com/ru/post/1685268/


All Articles