How to compare two typedesc in a pattern for equality

I would like to be able to compare two typedesc in a template to see if they refer to the same type (or at least have the same type name), but I'm not sure how. The operator ==

does not allow this.

type
  Foo = object
  Bar = object

template test(a, b: expr): bool =
  a == b

echo test(Foo, Foo)
echo test(Foo, Bar)

      

This gives me the following:

 Error: type mismatch: got (typedesc[Foo], typedesc[Foo])

      

How can I do that?

+3


source to share


1 answer


Operator is

helps: 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)

      

+3


source







All Articles