You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pivot.apache.org by Noel Grandin <no...@gmail.com> on 2009/04/12 16:14:08 UTC

why is Sequence not Iterable?

Hi

I was quite surprised when I called a method on a widget that returns
a sequence, and could not iterate over the Sequence<T>.

Is there a reason that Sequence<T> does not extend Iterable<T> - it
seems like most of the subclasses that implement Sequence do this
anyhow, and it would certainly make using it more convenient.

Regards, Noel Grandin.

Re: why is Sequence not Iterable?

Posted by Noel Grandin <no...@gmail.com>.
Well yes, they implement it, but those classes are not always exposed
in the API.

As an example, the selection listener for TreeView uses Sequence in
it's calback methods.

There are also other places where Component's return or accept
Sequence in the API.

On Sun, Apr 12, 2009 at 23:18, Greg Brown <gk...@mac.com> wrote:
> Most (if not all) classes that implement Sequence also implement Iterable,
> so, in practice, this isn't a big deal.
>
>
>

Re: why is Sequence not Iterable?

Posted by Greg Brown <gk...@mac.com>.
> So Map is Iterable, but Sequence is not?

Correct. List, Map, and Set are iterable, but Sequence, Dictionary,  
and Group are not (for the reason Todd mentioned earlier). The latter  
interfaces are also "lighter weight", allowing us to use them in a  
number of circumstances where List, Map, or Set would be too heavy.

> I can understand calling values() or keys() to retrieve something
> Iterable on a Map, because iterating over a Map is a little confusing
> - what am I iterating over - keys? values? key/value entries?

Map's iterator is defined to iterate over the key collection. It is  
similar to iterating over the properties of an object in JavaScript. I  
think this is cleaner than providing a "key set" property, and also  
works well since you can go from key to value, but you can't go the  
other way around.

> If Sequence is not going to be Iterable, perhaps we need another type
> in the heirarchy that is Iterable, so that those places in pivot.wtk
> which use Sequence in the API are easier to work with.

Most (if not all) classes that implement Sequence also implement  
Iterable, so, in practice, this isn't a big deal.



Re: why is Sequence not Iterable?

Posted by Noel Grandin <no...@gmail.com>.
So Map is Iterable, but Sequence is not?
I would have thought that it should rather be the other way around.

I can understand calling values() or keys() to retrieve something
Iterable on a Map, because iterating over a Map is a little confusing
- what am I iterating over - keys? values? key/value entries?

Whereas iterating over a Sequence is clear and un-ambiguous.

If Sequence is not going to be Iterable, perhaps we need another type
in the heirarchy that is Iterable, so that those places in pivot.wtk
which use Sequence in the API are easier to work with.

I nomitate IterableSequence, which is perhaps a little unwieldly, but
reasonably obvious :-)


On Sun, Apr 12, 2009 at 19:14, Todd Volkert <tv...@gmail.com> wrote:
> Greg can correct me if I'm wrong, but I believe it's to allow us to
> combine collections in ways that aren't possible in the JDK
> collections, like:
>
> class Foo implements Map<A, B>, Sequence<C>
>
> If Sequence (and Dictionary) extended Iterable, this type of class
> would be impossible.
>
> -T
>
> On Sun, Apr 12, 2009 at 10:14 AM, Noel Grandin <no...@gmail.com> wrote:
>> Hi
>>
>> I was quite surprised when I called a method on a widget that returns
>> a sequence, and could not iterate over the Sequence<T>.
>>
>> Is there a reason that Sequence<T> does not extend Iterable<T> - it
>> seems like most of the subclasses that implement Sequence do this
>> anyhow, and it would certainly make using it more convenient.
>>
>> Regards, Noel Grandin.
>>
>

Re: why is Sequence not Iterable?

Posted by Todd Volkert <tv...@gmail.com>.
Greg can correct me if I'm wrong, but I believe it's to allow us to
combine collections in ways that aren't possible in the JDK
collections, like:

class Foo implements Map<A, B>, Sequence<C>

If Sequence (and Dictionary) extended Iterable, this type of class
would be impossible.

-T

On Sun, Apr 12, 2009 at 10:14 AM, Noel Grandin <no...@gmail.com> wrote:
> Hi
>
> I was quite surprised when I called a method on a widget that returns
> a sequence, and could not iterate over the Sequence<T>.
>
> Is there a reason that Sequence<T> does not extend Iterable<T> - it
> seems like most of the subclasses that implement Sequence do this
> anyhow, and it would certainly make using it more convenient.
>
> Regards, Noel Grandin.
>