spicy
spicy copied to clipboard
Consider introducing a `map` function combining contains check and value retrieval
If one wants to check whether a map contains a value and retrieve it one currently is nudged into a double iteration pattern, e.g.,
global xs: map<uint64, string>;
xs[0] = "0";
xs[1] = "1";
xs[2] = "2";
global needle = 1;
if (needle in xs) # iterates for search.
print xs[needle]; # iterates for value retrieval.
We currently provide map::get which bundles this into one operation (though it also generates code which does a double iteration), but this can only be used for this use case if the there is some magic "default" value which cannot appear in the genuine value domain (else we couldn't distinguish entries at default from absent values).
We should consider introducing a map method (e.g., get_if or similar) which given a key returns an optional value (set if present, else unset). We could then also use this to provide a more efficient implementation of get.