How to compare two typedesc in a template for equality

543 views Asked by At

I'd like to be able to compare two typedesc in a template to see if they're referencing the same type (or at least have the same type name) but am not sure how. The == operator doesn't allow this.

type
  Foo = object
  Bar = object

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

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

It gives me this:

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

How can this be done?

1

There are 1 answers

0
def- On BEST ANSWER

The is operator 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)