How to use "maps" motoko package

Hello everyone, is there any links or resources about “maps” package in motoko?

The documentation is a bit of short and i don’t understand it because im a newbie here.

This example works

  let map = Map.new<Nat, Nat>();
  Map.set(map, nhash, 1, 1);

But when i do

  Map.put(map, nhash, 1, 1);

it gives me this error

cannot implicitly instantiate function of type
  <K, V>(Map__3<K, V>, HashUtils__3<K>, K, V) -> ?V
to argument of type
  ([var ?([var ?Nat], [var ?Nat], [var Nat], [var Nat32])],
   (Nat -> Nat32, (Nat, Nat) -> Bool), Nat, Nat)
to produce result of type
  ()
because no instantiation of K__195, V__162 makes
  ([var ?([var ?Nat], [var ?Nat], [var Nat], [var Nat32])],
   (Nat -> Nat32, (Nat, Nat) -> Bool), Nat, Nat)  <: 
    (Map__3<K__195, V__162>, HashUtils__3<K__195>, K__195, V__162)
and
  ?V__162  <:  ()Motoko(M0098)
1 Like

im assuming its this package?
https://mops.one/map

Motoko errors like this are super hard to read, so i have a few tricks i do to figure out what is really wrong

I would suggest with anything generic, to define the types temporarily like:

Map.put<Nat, Nat>(map, nhash, 1, 1);

That should help the error checking a bit to give you a better error message

My guess is that in this case you need to handle the return value, so

let oldValue = Map.put(map, nhash, 1, 1);

or

ignore Map.put(map, nhash, 1, 1);

Motoko doesn’t like it when you dont deal with the return value

1 Like