This is how yglukhov does it in variant:
proc mangledName(t: NimNode): string =
mangledNameAux(getTypeImpl(t)[1])
macro getMangledName(t: typed): string =
result = newLit(mangledName(t))
type TypeId* = Hash
macro getTypeId*(t: typed): TypeId =
let name = mangledName(t)
var h = hash(name) mod 2147483645
while true:
if h in typeIds:
if typeIds[h] == name: break
h = (h *% 2) mod 2147483645
else:
typeIds[h] = name
break
result = newLit(h)
Not sure if that's what you want but maybe it helps:
import std/macrocache
const nextTypeId = CacheCounter("nextTypeId")
type TypeId = int
func typeId*(T:typedesc): TypeId =
const id = nextTypeId.value
static:
inc nextTypeId
id
echo typeId(int)
echo typeId(string)
echo typeId(float)
echo typeId(int)