[erlang-questions] 2D Map Arrays
Wed Sep 13 15:58:38 CEST 2006
Mikael Pettersson wrote:
> 1. Doing a set on an index higher than the current maximum causes the
> array to grow, while doing a get on an index higher than the current
> maximum throws an exception. This makes sense for objects like partial
> mappings, but is quite strange for array-like objects. Perhaps a
> name change to "intmap" would capture its behaviour more accurately?
It depends on which arrays you use as a reference for your thinking. :-)
(Maybe I've looked a little too much at Python, Ruby and Perl lately,
but I quite like this behaviour.)
Automatic growing can of course lead to an undetected error, if you
happen to do a 'set' with an index that should have been out of bounds,
but that is probably more than made up for in the cases where you would
otherwise have to keep track of growing it yourself - and screwing up.
Throwing exceptions if you access an uninitialized element within
the current range would require a change in representation, wasting
both space and time, and is therefore right out.
An alternative to throwing an exception when accessing an index that
is larger than the current maximum, is to return the default value.
(For example, in Ruby you get a 'nil' if you do this.) But that gives
you a mismatch between what the array claims to be its current size,
and which indices you can access as if they contain a value. (And what
about negative indices? Some languages interpret this as offsets from
the end of the array, but I'm not sure that would very useful.)
Note that in our arrays, if you can read from index I, then you
can also read from index I-1, if I > 0. I think the name 'array'
remains a good match.
I suppose it would be possible to add a kind of locking to arrays,
so that locked/fixed-size arrays are not allowed to grow automatically.
Is this a much wanted feature, folks?
More information about the erlang-questions