3

テンプレート内の 2 つの 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 に答える