2015-06-17 52 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

這是完美的。謝謝! –

相關問題