2015-06-17 55 views
3

我希望能够比较模板中的两个typedesc以查看它们是否引用相同类型(或至少具有相同类型名称)但不是当然如何。 ==运营商不允许这样做。如何比较模板中的两个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]) 

如何才能做到这一点?

回答

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) 
+0

这是完美的。谢谢! –

相关问题