Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can the "static type" and "dynamic type" be different?

Tags:

types

nim-lang

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.

like image 329
dgo.a Avatar asked Jan 30 '23 15:01

dgo.a


1 Answers

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
like image 146
def- Avatar answered Feb 03 '23 13:02

def-