Searching
The predicate Big_map.mem
tests for membership in a given big map,
given a purported key.
const my_map: big_map<int,string> =
Big_map.literal(list([[1,"one"],[2,"two"]]));
const contains_2: bool = Big_map.mem(2, my_map); // == true
In practice, however, we would like to get the value associated to the
key we searched. This is achieved by means of Big_map.find_opt
.
const v : option<string> = Big_map.find_opt(2, my_map);
Notice how the value we read is an optional value: this is to force the reader to account for a missing key in the big map. This requires pattern matching.
let force_access = (key, map) => {
return match(Big_map.find_opt (key, map)) {
when(Some(value)): value;
when(None): failwith("No value.")
};
};
In fact, the predefined function Big_map.find
does exactly that,
except that the exception raised by failwith
carries the default
string "MAP FIND"
.