Indexing access for Lists and Maps considered harmful?
Neal Gafter
neal at gafter.com
Mon Jun 22 12:42:56 PDT 2009
I don't think the indexing operator has to be defined in a way that isn't
parallel to the way indexing works with ordinary arrays. I would expect the
result of the expression
map["b"] = "c"
to be the value "c". We can have that if the construct is defined by
translation as:
var tmp = "c";
map.put("b", tmp);
tmp
What is the point, you might ask, of the interface definition of "put"
returning a value that the language always ignores? My answer is that this
is an error in the specification; a language construct such as the indexing
operators that can be defined by a syntactic translation can ignore the
types. That simplifies the specification and the implementation, and it
allows the operator to be more widely retrofitted.
The same comment applies to the ARM proposal: a pattern based approach would
make it more widely applicable and retrofittable, especially with extension
methods. This is one of the most important (but usually ignored) benefits
of extension methods.
Regards,
Neal
On Mon, Jun 22, 2009 at 12:19 PM, Joseph D. Darcy <Joe.Darcy at sun.com> wrote:
> Hello.
>
> A recent blog post cited in Alex Miller's handy "Java 7 Links"
> (http://java7.tumblr.com) claims that adding indexing support for Lists
> and Maps would be harmful because of an arguably surprising behavior on
> compound assignment. After
>
> map["a"] = map["b"] = "c";
>
> the new value for key "a" would be the old mapping of "b" and not
> necessarily "c" because a call to the put method returns the old value.
>
> I'm not too concerned about this interaction because compound assignment
> is relatively infrequent; however, I think a lint warning from the
> compiler would be appropriate in this case. Also, an IDE could color
> the non-array uses of "[]" differently to highlight any potential
> differences in semantics.
>
> As a meta-comment, I find it odd that if someone wanted to inform the
> coin discussion he or she would not directly email the coin list in
> addition to or instead of posting a blog entry.
>
> -Joe
>
>
More information about the coin-dev
mailing list