Apparently typedesc arguments are covariant. For example:
import typetraits
type
A = object of RootObj
B = object of A
proc f(T: typedesc[A]) =
echo "should only accept A, but not B"
echo "T is A: ", T is A
echo "name(T): ", name(T)
f(A)
f(B) # how to prevent this to compile?
Run
This compiles and prints:
should only accept A, but not B
T is A: true
name(T): A
should only accept A, but not B
T is A: true
name(T): B
Run
Is there an elegant solution so that I cannot accidentally invoke `f(B)`? The
ideal solution (in my particular problem) would be to avoid the function match
entirely. Otherwise some when + error statements might be needed, but `when T
is A` doesn't work because it also returns true for subtypes, `when name(T) ==
name(A)` feels hacky.