Currently, one needs to use
my_map.Has(key) if the programmer is unsure of a key being in the map or not. However, I'm pretty sure the internal code for
Has() most likely needs to do a
full key search to return the answer. And almost certainly, the programmer is going to have code that looks like this:
Code: Select all
if( my_map.Has(my_key) )
my_var := my_map[my_key]
This means the lookup is done twice for every lookup, when uncertain. I propose a new ability to safely scope out map elements and obtain their value at the same time when they exist. If I were developing the system, I would also add (any/all of) the following features to map objects:
- A safe map.Get(my_key,my_default) function that returns the user's default value when the key doesn't exist.
- A map.SetIfMissing(my_key,my_value) type function that only assigns the value if the key doesn't exist (probably with a better function name).
- A map.GetSet(my_key,start_value) type function that assigns start_value only when the key doesn't exist, then returns the current key value afterward.
Thanks for reading my wishes!