AA default value?
Janice Caron
caron800 at googlemail.com
Fri Jan 25 04:11:26 PST 2008
The big problem for me with associative arrays is that I often have to
write ugly code like:
auto p = key in aa;
if (p)
{
auto val = *p;
}
else
{
/* whatever */
}
instead of merely
auto val = aa[key];
In order to avoid superfluous lookups, the code that I actually end up
writing doesn't use the square brackets at all, which kind of defeats
the object of the built-in array-like syntax. What would seriously
make for nicely readable code would be if the behavior of aa[key]
could be changed. Currently, aa[key] throws an exception (in Debug
mode) or falls over (in Release mode) if key does not exist. Wouldn't
it be nicer if, instead, it simply returned
typeof(aa[key]).init
if the key was not present in the array? Or perhaps an even more
exotic syntax like
auto val = aa[key; defaultValue];
might work.
(None of this would preclude people from using "in" if they explicitly
wanted to check).
More information about the Digitalmars-d
mailing list