himanshudixit / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

UnmodifiableIterable? #280

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm tempted to use UnmodifiableIterator, but perhaps I would really want an 
UnmodifiableIterable instead. How come this is not included?

public abstract class UnmodifiableIterable<E> implements Iterable<E> {
    public abstract UnmodifiableIterator<E> iterator();
}

Original issue reported on code.google.com by jim.andreou on 29 Oct 2009 at 7:12

GoogleCodeExporter commented 9 years ago
Oh, I see you ended up having ImmutableCollection's iterator() method return an 
UnmodifiableIterator. I see no harm in factoring it in a superclass, if for 
example one 
implements an immutable container but would rather not implement size(), 
contains() 
etc, but granted, I don't see major benefit from this either.

Original comment by jim.andreou on 29 Oct 2009 at 7:34

GoogleCodeExporter commented 9 years ago
We considered this once and we weren't clear on the major benefits either, 
which made 
it feel gratuitous. Also, it's potentially ambiguous, since 'collection 
instanceof 
UnmodifiableIterable' doesn't imply that it's an unmodifiable collection (only 
that it 
can't be modified via its iterator).

Original comment by kevin...@gmail.com on 29 Oct 2009 at 7:51

GoogleCodeExporter commented 9 years ago

Original comment by kevin...@gmail.com on 30 Oct 2009 at 4:01

GoogleCodeExporter commented 9 years ago
I was just about to start a new issue for that.

I want to argue in favour of UnmodifiableIterable for two reasons I see as major
benefits:
 - consitency: If you sprinkle Unmodifiable* and Immutable* all around your code, it
is better to have a clear marker interface that states that a returned iterable 
is
unmodifiable as well. Furthermore seeng it in a method signature is a clear 
statement
that the method is not going to try invoking remove on the itarebles iterator.

 - Principle of least surprise. If your clearly type all return values as
UnmodifiableIterable and get only a UnmodifiableIterator out of it, you will 
never be
tempted to use it wrong.

Please reconsider adding the Type.

Original comment by marcus.t...@gmail.com on 1 Nov 2009 at 10:03