gpt4 book ai didi

types - "static type"和 "dynamic type"怎么可能不同?

转载 作者:行者123 更新时间:2023-12-03 19:06:56 31 4
gpt4 key购买 nike

根据 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: 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

关于types - "static type"和 "dynamic type"怎么可能不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46106812/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com