This code basically does what I may want
type
O[X] = object
i: int
x: X
proc d[X](o: O[X]) =
when type(o.x) is bool: echo "yes" else: echo "no"
var h1: O[bool]
var h2: O[int8]
d(h1)
d(h2)
yes no
But of course it is strange to use an arbitrary data type field just to get a compile time decision.
The reason why I may want that is, that I have generic data types, which for special use cases allow optimizations. But I do not want to code all procs working on these data types twice, and I can not use "when (defined)" because decision is for individual instances. Indeed it is for a Robin-Hood Hash Table, where for some cases optimizations are possible. But the optimization is not really based on the key data type, but more on the used equality test, so something like "when type(key) is string" is not the best solution. Well, maybe I will find a solution in the manual...
[EDIT]
Well, I could delete this posting: :-)
From manual: "The is operator checks for type equivalence at compile time." So it is exactly what I need. I was confused, thought it is a runtime check. Confused is operator with of operator!
If I understand your problem correctly, you can just use overloading for the more specific type. You can also avoid having the data x:
proc d[X](o: O[X]) = echo "no"
proc d(o: O[bool]) = echo "yes"
So I think my code should have a shape like this, that should be OK:
type
O[K] = object {.inheritable.}
i: K
O2[K] = object of O[K]
h: int # special field for optimizations
proc d[K](o: O[K] | O2[K]) =
when o is O2: echo "doing optimizations"
var h1: O[int]
var h2: O2[int]
d(h1)
d(h2)