Types “怎么可能?”;“静态类型”;及;“动态类型”;会有所不同吗?
根据Nim手册,变量类型为“静态类型”,而变量在内存中指向的实际值为“动态类型” 它们怎么可能是不同类型的呢?我认为给变量分配错误的类型是错误的Types “怎么可能?”;“静态类型”;及;“动态类型”;会有所不同吗?,types,nim-lang,Types,Nim Lang,根据Nim手册,变量类型为“静态类型”,而变量在内存中指向的实际值为“动态类型” 它们怎么可能是不同类型的呢?我认为给变量分配错误的类型是错误的 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: Pe
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