[Dojo-interest] [Head's Up] Collections are going to have
Cameron Braid
cameron at braid.com.au
Fri Apr 14 09:47:13 MDT 2006
I'm glad to hear someone else, who is much more eloquent than I, sharing
my thoughts.
Cameron.
On Fri, 2006-04-14 at 11:29 -0400, Drye, Stephen wrote:
> I think the last point here needs to be addressed. Collections
> can contain nulls (there are many situations where you want to do
> that), so iterators must be able to return null and not have current()
> return null for the end of the list.
>
> If current returns null, then in the situation where a developer is
> more familiar with the while(e.current()) idiom they WILL cause a bug
> when they try to use a collection with nulls in it (they'll stop
> iterating too early in the collection). If you have to change idiom
> to code a fairly common case in an API without introducing a bug, that
> API is broken.
>
> I also agree that current() sounds too much like a method that will
> only return the current item every time it is called, not return the
> current item and advance. In every normal English use of the word
> "current" it means the current/present/immediate item/task/etc. In
> real life when you ask someone what their current task is, do they
> tell you what it is and then immediately drop that task and go on to
> the next one? :) Again, it's setting itself up to be bug prone. If
> you're determined to use this idiom, the method name has to be changed
> to something more obvious like getCurrentAndAdvance(). For example,
> the PHP standard library's iterator has a current method, and it only
> returns the current item without advancing. That's what people are
> going to expect. Using "next" as the method name would clear up this
> confusion.
>
> An iterator is not the place to be blazing your own trail in API.
> It's probably beneficial to look at pre-existing iterator idioms in
> other languages before making this change.
>
> Sample iterator APIs:
> PHP: rewind, current, next, valid
> Java: hasNext, next
>
> C#: Reset, MoveNext, IsDone, Current
>
> Python: next (throws Exception at end)
>
> In the cases where languages have a current on their iterator, NONE
> have it return the item and advance. No-one is going to expect that
> behavior, so from a usability perspective the method fails the
> "principle of least surprise".
>
> Collections are going to be commonly used, so this will probably be
> the last time a compatibility-breaking change will be allowed as Dojo
> gets used widely. It's worth taking the time to get it right.
>
>
>
>
> ______________________________________________________________________
> From: dojo-interest-bounces at dojotoolkit.org
> [mailto:dojo-interest-bounces at dojotoolkit.org] On Behalf Of Cameron
> Braid
> Sent: Friday, April 14, 2006 10:10 AM
> To: dojo-interest at dojotoolkit.org
> Subject: Re: RE: [Dojo-interest] [Head's Up] Collections are going to
> have
>
>
>
>
> I still much prefer the java syntax ;
>
> while (e.hasNext() {
> var item = e.next();
> }
>
> I just think that current() makes it sound like you can call it
> multiple times to get the current record.
>
> I don't think iterators are meant to have a current record.. they are
> there to be a source of new records, which is why hasNext/next are the
> most suitable method names.
>
> I don't like this current() atEnd() and .element syntax.
>
> I'd implement is such that next() returns the next element, if one
> exists, otherwise it should throw an exception, not return null.
>
> Cameron.
>
> On Thu, 2006-04-13 at 12:21 -0500, Tom Trenka wrote:
>
> > If you intentionally populate a collection with null values (which I'm not sure why you'd do but is certainly possible), you'll want to use the atEnd() method for the test as opposed to current:
> >
> > while(!e.atEnd()){
> > var item=e.current(); // get and advance
> > }
> >
> > Hopefully it means you can have your cake and eat it, too.
> > ---------------------------------------------------------------------
> > Posted via Jive Forums
> > http://forums.opensymphony.com/thread.jspa?threadID=25809&messageID=50945#50945
> >
> > _______________________________________________
> > Dojo FAQ: http://dojo.jot.com/FAQ
> > Dojo-interest at dojotoolkit.org
> > http://dojotoolkit.org/mailman/listinfo/dojo-interest
>
>
> ______________________________________________________________________
> Cameron Braid
> PGP Public Key http://braid.com.au/cameron.pgp
>
> _______________________________________________
> Dojo FAQ: http://dojo.jot.com/FAQ
> Dojo-interest at dojotoolkit.org
> http://dojotoolkit.org/mailman/listinfo/dojo-interest
________________________________________________________________________
Cameron Braid
PGP Public Key http://braid.com.au/cameron.pgp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mastodon.dojotoolkit.org/pipermail/dojo-interest/attachments/20060414/961fbc68/attachment.html
More information about the Dojo-interest
mailing list