On 23 Mar 2011 20:53, "Cédric Beust ♔" <[email protected]> wrote: > > > > On Wed, Mar 23, 2011 at 1:23 PM, Russel Winder <[email protected]> wrote: >> >> Unlike Java which >> chose to ignore iterators à la C++ -- another paradigm war but this one >> was fought in the late 1990s. > > > First of all, before the enhanced for loop came out, Java was using iterators pretty much everywhere in collection loops: > > for (Iterator it = collection.iterator(); it.hasNext(); ) { > Object o = it.next(); > } > > This followed the techniques made popular by the STL, but it was seen as not just a lot of boiler plate but also exposing users to needless implementation details: if all you want is enumerating the objects in a collection, why should you even care about the concept of iterators? > > Thus, the enhanced for loop was born: > > for (Object o : collection) { > ... > } > > No more iterators, much easier to read, and the only objects involved in this construct are the ones you care about: the collection and the objects it contains. >
Of course there are iterators, they're just made implicit through a bit of syntactic sugar. Ultimately, the name of this construct is a dead giveaway, it's a for "loop". The thing is fundamentally imperative, and only has value through iterating over every element in a collection and optionally performing some side effect. If it were to return a value of some sort, then it's possible to imagine a future version of Java in which iterators weren't involved, but that simply ain't the case right now. > >> >> Joshua Bloch has at least acknowledged >> that the Java Collections framework took the wrong architectural >> decision; > > > Uh, citation? We came up with the enhanced for loop precisely to hide iterators, so I still claim that the enhanced for loop is a big improvement over what we had before. > > Or are you referring to something else? > > -- > Cédric > > -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
