Your example seems to work fine with 0.17.2, idOfType is only called once per T
import typetraits
type
Message[T] = object
proc idOfType*(T: typedesc): int {.inline.} =
echo("Getting Id of type ", T.name)
when T is int:
0
else:
1
proc sendMsg*[T](m: ptr Message[T]): void {.inline.} =
let typeIdOfT {.global.} = idOfType(T)
echo("Sending message with type Id ", typeIdOfT)
proc test() =
var intMsg: Message[int]
var stringMsg: Message[string]
sendMsg(addr(intMsg))
sendMsg(addr(stringMsg))
sendMsg(addr(intMsg))
sendMsg(addr(stringMsg))
test()
Output:
Getting Id of type int
Getting Id of type string
Sending message with type Id 0
Sending message with type Id 1
Sending message with type Id 0
Sending message with type Id 1
Sounds like a bug, I think. What version of the compiler are you using?