Nat and Int: hash is deprecated - Example of a "bespoke hash function"?

So the field hash for Nat and Int is deprecated but I need a TrieMap with a Nat as its key. Problem is, I really don’t know how to write a hash function or what to consider when hashing a Nat. I thought the following would work, but it’s giving me both the warning about the deprecated hash field and an error:

let myDB = TrieMap.TrieMap<Nat, Text>(Nat.equal, Hash.hash(Nat));

Any ideas or best practices?

Those hash functions are deprecated because they only look at the lower 32 bits rather than hashing all the bits. That might be fine for your
application if the nats are small anyway.

I think the type error is due to the Hash.hash(Nat). Replace by just Hash.hash.

On mobile so couldn’t verify fix.

1 Like

This solves it for me, thank you!

Simply using Hash.hash actually resolved the issue for you? We still get the deprecation warning when using it as follows:

 let m = Map.fromIter<dataTokenID, [tokenID]>(
    tokens.vals(),
    10,
    Nat.equal,
    Hash.hash,  // warning: hash is deprecated
  );

Perhaps Hash.hash is still only using the lower 32 bits in this particular context?

Hi Aaron! I just went ahead and ignored the warning since I’m only dealing with low values anyway. The reason I decided to stick with Nat instead of changing the type to Nat32 was - if I’m not mistaken - that Nat still has a lower memory footprint than Nat32. At least that’s what I read somewhere.