我希望能够比较模板中的两个typedesc,看看它们是否引用相同的类型(或者至少具有相同的类型名称),但我不确定如何。 ==
运算符不允许这样做。
type
Foo = object
Bar = object
template test(a, b: expr): bool =
a == b
echo test(Foo, Foo)
echo test(Foo, Bar)
它给了我这个:
Error: type mismatch: got (typedesc[Foo], typedesc[Foo])
如何做到这一点?
答案 0 :(得分:3)
is
运算符有助于:http://nim-lang.org/docs/manual.html#generics-is-operator
type
Foo = object
Bar = object
template test(a, b: expr): bool =
#a is b # also true if a is subtype of b
a is b and b is a # only true if actually equal types
echo test(Foo, Foo)
echo test(Foo, Bar)