Indexing access for Lists and Maps considered harmful?

Neal Gafter neal at gafter.com
Tue Jun 23 17:47:34 PDT 2009


These methods are language-support methods.  If I create a new array-like
API, what should its "put" method return?  Four choices are void, null, the
input value, or the logical value placed into the data structure.

void doesn't work because that's not the return type of the method in these
interfaces.

null is silly (an API method that should always return nothing should be
declared void), and conflicts with the JLS's meaning of an assignment
expression (see below).

The input value isn't correct; the value resulting from the assignment is
supposed to be the NEW value in that variable (JLS3 15.26 paragraph 3).  For
built-in arrays, that is the value converted to the array element type.

For general collection-like APIs, the result of an assignment using the
array syntax may be some other "equivalent" value (for example, strings
might be interned).  To support the JLS semantics, the API needs to have an
opportunity to yield this value.  So IndexedAccess<V>.put() should be
declared to return a value of type V, which should be specified to be *the
value of the logical variable after the assignment has occurred*.
java.util.List, on the other hand, has a put method that is declared with
the right signature (it returns a V), but it returns the wrong value to
support the JLS semantics for assignment.

Retrofitting interfaces onto existing classes is a poor solution to this
language-design problem.

Regards,
Neal

On Tue, Jun 23, 2009 at 4:35 PM, Shams Mahmood <shams.mahmood at gmail.com>wrote:

> I don't follow why it won't handle anything other than the collections api.
> The compiler should be able to support any implementations of
> IndexedAccess<V> and DictionaryAccess<K, V> just like the for each loop
> handles any implementations of the Iterable interface.
>
>
>
> ------------------------------
> *From:* Neal Gafter <neal at gafter.com>
> *To:* Shams Mahmood <shams.mahmood at gmail.com>
> *Cc:* coin-dev at openjdk.java.net
> *Sent:* Tuesday, June 23, 2009 4:42:40 PM
> *Subject:* Re: Indexing access for Lists and Maps considered harmful?
>
> On Tue, Jun 23, 2009 at 1:55 PM, Shams Mahmood <shams.mahmood at gmail.com>wrote:
>
>> So,
>> List<String> list = new ArrayList();
>> list[1] = list[0] = "value";
>> would get translated to:
>> Collections.set(list, 1, Collections.set(list, 0, "value"))
>
>
> The link to methods in Collections is a bit too magical (e.g. it won't
> gracefully handle anything other than the collections API nor will it
> gracefully support evolution of the platform).
>
>
>



More information about the coin-dev mailing list