Maps, (AKA dictionaries), provide key-value data association. Ion has two variants of maps: Hash and BTree. Hash maps are fast but store data in a random order. BTree maps are slower, but keep their data in a sorted order. If not sure what to use, go with Hash maps.

Creating maps uses the same right-hand-side array syntax. However for design simplicity, users must annotate the type to translate the array into a map.

Please note, the map's inner type specifies the value's type and not of the key. Keys will always be typed str.


let hashmap:hmap[str] = [ blue=pc27 red=pc2 green=pc15 ]
let x = blue
echo @hashmap[$x] @hashmap[red] # fetch values
let hashmap[orange] = pc22 # add new key with value
#echo @keys(hashmap) #get keys
#echo @values(hashmap) #get values
#echo @hashmap #get keys and values
#for key value in @hashmap #use keys and values
#  echo $key: $value
pc27 pc2


let btreemap:bmap[str] = [ pc2=red pc15=green pc27=blue ]
let x = pc2
echo @btreemap[$x] @btreemap[pc15] # fetch values
let btreemap[orange] = pc22 # add new key with value
echo @keys(btreemap) #get keys
echo @values(btreemap) #get values
echo @btreemap #get keys and values
for key value in @btreemap #use keys and values
  echo $key: $value
red green
orange pc15 pc2 pc27
pc22 green red blue
orange pc22 pc15 green pc2 red pc27 blue
orange: pc22
pc15: green
pc2: red
pc27: blue