Indexing access for Lists and Maps considered harmful?
Ted Neward
ted at tedneward.com
Wed Jun 24 00:48:50 PDT 2009
Um.. I guess I assume that the getter would work as expected. I don't really
care what the underlying code generated looks like, so long as the language
semantics are upheld. Frankly, if the compiler wants to turn this:
list[0] = list[1] = "hello";
into:
T temp = "hello";
list.indexSet(1, temp);
list.indexSet(0, temp);
as opposed to:
list.indexSet(1, "hello");
list.indexSet(0, list.indexGet(1));
on the grounds that the first version would be moderately faster, I could
really care less. That's an implementation detail.
The goal of any language enhancement should be to preserve existing language
semantics even as it extends them.
Ted Neward | Principal Consultant, ThoughtWorks
Java, .NET, XML Services
Consulting, Teaching, Speaking, Writing
http://www.thoughtworks.com <http://www.tedneward.com> |
http://www.tedneward.com
From: spamolovko [mailto:spamolovko at gmail.com]
Sent: Wednesday, June 24, 2009 12:33 AM
To: Ted Neward
Cc: 'Reinier Zwitserloot'; coin-dev at openjdk.java.net
Subject: Re: Indexing access for Lists and Maps considered harmful?
How must work "list[0] = list[1];"?
this must work as:
list.indexSet(0, list.indexGet(1));
So
list[0] = list[1] = "hello";
must work same as semantically identical
list[1] = "hello";
list[0] = list[1];
This mean that it would be translated into:
list.indexSet(1, "hello");
list.indexSet(0, list.indexGet(1));
Why are you forgetting about GETTER? If you get anything from collection -
you MUST use getter, not result from setter. That is JavaBeans semantic.
PS do not use setter instead of getter and all will work fine.
Ted Neward wrote:
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
More information about the coin-dev
mailing list