3

我希望能够比较模板中的两个 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])

如何才能做到这一点?

4

1 回答 1

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)
于 2015-06-17T19:03:37.837 回答