Indexing access for Lists and Maps considered harmful?

Ted Neward ted at tedneward.com
Tue Jun 23 23:52:56 PDT 2009


> public interface IndexedAccess<ValueType> {
> 
>     public ValueType indexGet(int key);
> 
>     public void indexSet(int key, ValueType value);
> 
> }
> 
> interface Collection<E> extends Iterable<E>, IndexedAccess<E>
> 
> and then just use:
> List<String> list = new ...
> list[0] = "hello";
>
> What is the problems here?
>
For starters, assuming your implementation turns

list[0] = "hello";

into

list.indexSet(0, "hello");

which returns void, then the following

list[0] = list[1] = "hello";

would fail:

list.indexSet(0, list.indexSet(1, "hello"));
  // error! Cannot pass void as a parameter

And that would break the existing Java semantics that have been there since
1.0.

In your implementation, indexSet *must* return ValueType, and it must return
the value passed in, if the existing semantics for Java are going to be
preserved. Am I missing something here? If, as I think Neal already pointed
out, we look at the expression

a = b = c;

The results of the intermediate expression "b = c" is "c", and therefore the
results of the expression "a = (b = c)" is "c" as well. What are we arguing
about? This is established Java language semantics, isn't it?

Ted Neward | Principal Consultant, ThoughtWorks
Java, .NET, XML Services
Consulting, Teaching, Speaking, Writing
http://www.thoughtworks.com | http://www.tedneward.com

> -----Original Message-----
> From: coin-dev-bounces at openjdk.java.net [mailto:coin-dev-
> bounces at openjdk.java.net] On Behalf Of spamolovko
> Sent: Tuesday, June 23, 2009 11:50 PM
> To: Reinier Zwitserloot
> Cc: coin-dev at openjdk.java.net
> Subject: Re: Indexing access for Lists and Maps considered harmful?
> 
> Reinier Zwitserloot wrote:
> > I agree to the theory of your post, but, so what?
> >
> > Java is not going to be perfect unless java is willing to sacrifice
> > backwards compatibility, which it isn't. So, perfection can't be the
> > goal - because if it is, then no language change is ever going to
> make
> > it in, and paradoxically, that really brings java no closer to
> > perfection.
> >
> Yes, that is true. I hope that Oracle will "sacrifice backwards
> compatibility" and implement features that are standard for other good
> languages.
> 
> And being "non perfect" is not a reason to stop evolution.
> 
> > So - so what that it isn't perfect? Let's roll with where I think you
> > are trying to go, and create 2 interfaces, where the SetIndex
> > semantics are defined as requiring the implementing class to return
> > the logical value - e.g. boxing a primitive when assigning it into a
> > list, for example.
> >
> > But now we can't retrofit java.util.List without breaking thousands
> of
> > List implementations out there, so the conclusion would have to be
> > that you can not use a List itself; you must use either a specific
> > type (such as ArrayList, retrofitted to implement SetIndex), or a
> > newly created List2 interface that also implements SetIndex. In this
> > scenario, I'm assuming the name of the method isn't set(), as that
> > would conflict with java.util.List's set which has different
> semantics.
> >
> public interface IndexedAccess<ValueType> {
> 
>     public ValueType indexGet(int key);
> 
>     public void indexSet(int key, ValueType value);
> 
> }
> 
> interface Collection<E> extends Iterable<E>, IndexedAccess<E>
> 
> and then just use:
> List<String> list = new ...
> list[0] = "hello";
> 
> What is the problems here?
> >
> >   --Reinier Zwitserloot
> >




More information about the coin-dev mailing list