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)
    
    
    Run

Reply via email to