3

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?

Lye Fish
  • 2,538
  • 15
  • 25

1 Answers1

3

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)
def-
  • 5,275
  • 20
  • 18