多重方法 (Multi-methods)
Note 从 Nim 0.20 开始,要启用多重方法,开发者必须在编译时显式添加 --multimethods:on 参数。
在多重方法中,所有对象类型的参数都会用于方法派发:
type
Thing = ref object of RootObj
Unit = ref object of Thing
x: int
method collide(a, b: Thing) {.base, inline.} =
quit "to override!"
method collide(a: Thing, b: Unit) {.inline.} =
echo "1"
method collide(a: Unit, b: Thing) {.inline.} =
echo "2"
var a, b: Unit
new a
new b
collide(a, b) # 输出: 2
当前内容版权归 vectorworkshopbaoerjie 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 vectorworkshopbaoerjie .