Dictionary in motoko

Hey there,

Is there a some form of a dictionary in motoko? I seem to remember from a demo video that it was possible but when looking in the language guide around mutable state section and the standard library I can’t seem to find a dictionary structure.

Edit: just noticed these line in the linkedup code

import Trie "mo:stdlib/trie.mo";
type Trie<Key, Value> = Trie.Trie<Key, Value>;

This Trie type seems to have a key value structure but I can’t find any documentation around it

3 Likes

Seems like the documentation isn’t up to date yet. Did you manage to make it work?

1 Like

I saw this by Hans

Does this help?

4 Likes

^^ That should do it, also see here for docs: Key-Value pairs in Motoko

4 Likes

Very nice resources thank you!

2 Likes

Hmm when trying the phonebook actor code I get the following error:

  IDL generation returned an error:
/Users/dfinitybasics/hello/src/hello/main.mo:1.1-1.26: import error, file "/Users/dfinitybasics/.cache/dfinity/versions/0.4.13/stdlib/list" does not exist
/Users/dfinitybasics/hello/src/hello/main.mo:2.1-2.31: import error, file "/Users/dfinitybasics/.cache/dfinity/versions/0.4.13/stdlib/assocList" does not exist

Does anyone know what could cause this? I’m using the exact same import statements as in the phonebook example. I double checked for capital letters

Edit: I fixed it by putting .mo behind the import statements like this

import L “mo:stdlib/list.mo”;
import A “mo:stdlib/assocList.mo”;

Does anybody have the same problem because then maybe the phonebook example should be updated.

Edit2: seems like the dfinity-lab repo code does have .mo in the source code but the developers guide https://sdk.dfinity.org/developers-guide/tutorials/phonebook.html doesn’t

2 Likes

Yes the import in the example there does need updating (it’s been flagged).

The .mo extension will likely be removed from imports on the next release too so keep that in mind, you’ll notice the docs are being updated to reflect this.

2 Likes

Yes the import in the example there does need updating (it’s been flagged).

Everyone feel free to ping support@dfinity.org with any bugs or documentation errors you find!

2 Likes