Indexing access for Lists and Maps considered harmful?
Reinier Zwitserloot
reinier at zwitserloot.com
Tue Jun 23 12:27:59 PDT 2009
Yes, Artur, I expect there will be very many classes that implement
one but not the other.
Immutables for the win.
As this is going to be a 'magic interface', in a sense, in that the
JLS explicitly refers to that exact interface (similar to how
java.lang.Iterable is magic) it has to be 'right', from now until
perpetuity. The current java collections API is fundamentally not
designed with immutability in mind (a design that works with mutable/
immutable probably has a List superinterface that has only getter
methods, then an ImmutableList subinterface that's used as marker, and
a MutableList that adds the setters, or some such) - unlike Iterable
and the proposed AllowGetAccess (or whatever it ends up being called)
- the Java collections API is not part of the JLS, and some people
have indeed switched to other collections APIs, such as google's, or
even javac's own internal cons-style List version which is used
extensively in javac instead of ArrayList and friends.
--Reinier Zwitserloot
On 2009/23/06, at 20:02, Artur Biesiadowski wrote:
> Joseph D. Darcy wrote:
>> // New superinterfaces to indicate syntax?
>> interface GettableFromInt<E> {
>> E get(int);
>> }
>>
>> interface Gettable<K, V> {
>> V get(K k);
>> }
>>
>> interface Settable<E> {
>> E set(int, E e);
>> }
>> interface Puttable<K, V> {
>> V put(K k, V v);
>> }
>>
> If anything like that is implemented, I would rather see it as two
> interfaces - ListAccess and MapAccess (or maybe IndexAccess and
> Key/DictionaryAccess to be less collection-oriented?). Read-only or
> write-only classes can throw runtime exceptions if needed.
>
> Do you think that one-operation classes would be common? I can see a
> possible benefit of detecting this at compile time instead of runtime,
> but how useful it would be in real world...
>
> All collection classes would be read-write (even for immutable
> collections no way to indicate it on interface level).
> String could be probably made 'GettableFromInt'.
> StringBuilder/Buffer is already read-write. Same goes for BitSet.
>
> I think it boils down to question if we want to have
>
> String txt = "ABC";
> txt[0] = 'X';
>
> failing at compile time or runtime. On the other hand, in case of
> String, I could imagine most IDEs making it a warning at compile time
> easily...
>
> Regards,
> Artur Biesiadowski
>
More information about the coin-dev
mailing list