Hi I know this is slightly off topic but f there is a discussion on enhancing the for each loop ...
1 is there any chance that we could use ARM functionality and allow a ClosableIterator to be introduced. There are many occasions that an iterator is backed by some resource (list of lines in a file, list of rows in a database) but there is no mechanism in the foreach loop to close this implicit resource. This was one thing that C# got right in its implementation, if the iterator implements IDisposable then it is disposed. In java it may require an additional interface although I doubt that any current Iterators implement Closable 2 it would be nice if the foreach loop honoured the RandomAccess semantics but it is probably too late for that now Regards Mike Sent from my BlackBerry® wireless device -----Original Message----- From: Andrew Thompson <lordpixel+core-libs-...@mac.com> Sender: core-libs-dev-boun...@openjdk.java.net Date: Wed, 24 Aug 2011 20:50:07 To: Collin Fagan<collin.fa...@gmail.com> Cc: core-libs-dev@openjdk.java.net<core-libs-dev@openjdk.java.net>; lambda-dev<lambda-...@openjdk.java.net> Subject: Re: Enumeration adapters in SE 8 The key advance would be the last one, unfortunately the one Dan says may not make it: allowing the for loop to take an Iterator. I had some legacy code in the last couple of weeks where I wanted to be able to process arbitrary sequences: arrays, iterables and iterators and I ended up copying and pasting control flow code because there's no unifying interface. Enumerations are nice to have. Although I can use the for loop with an array, there's no way to write a generic method to handle arrays and Iterables. Arrays that implement Iterable would be more valuable if we're talking convenience. On Aug 24, 2011, at 6:13 PM, Collin Fagan <collin.fa...@gmail.com> wrote: > There was a time where it was considered best practice to return an iterator > instead of a collection. There are now many apis and legacy code that are > not compatible with the for each loop. > > I think even if there are only a few places in the jdk where this would > apply it would still be convenient in the wild. > Collin > > On Wed, Aug 24, 2011 at 4:58 PM, Colin Decker <cgdec...@gmail.com> wrote: > >> I've come across various scenarios. One example is >> javax.swing.tree.DefaultMutableTreeNode. It has numerous methods that >> return >> Enumerations, including: >> - children >> - depthFirstEnumeration >> - breadthFirstEnumeration >> - preorderEnumeration >> - postorderEnumeration >> - pathFromAncestorEnumeration >> >> I've created a utility class with methods to return Iterables for each of >> these types of Enumerations for convenience, but it would be much nicer to >> just use a method reference. >> >> Deque's descendingIterator() method is a more modern good candidate for >> treating as an Iterable. See this question on StackOverflow: >> >> http://stackoverflow.com/questions/3883131/idiomatic-way-to-use-for-each-loop-given-an-iterator >> >> -- >> Colin >> >> >> On Wed, Aug 24, 2011 at 5:45 PM, Ben Evans <benjamin.john.ev...@gmail.com >>> wrote: >> >>> Hi Dan, >>> >>> On Wed, Aug 24, 2011 at 7:10 PM, Dan Smith <daniel.sm...@oracle.com> >>> wrote: >>> >>>> I was pointed to some comments on core-libs about adapting Enumerations >>> to >>>> for loops in SE 8. (Sorry for the new thread -- I wasn't subscribed.) >>> It >>>> turns out lambdas + extension methods will make this very easy. >>>> >>>> In the API: >>>> >>>> interface Enumeration<E> extends Iterator<E> { >>>> boolean hasMoreElements(); >>>> E nextElement(); >>>> boolean hasNext() default #hasMoreElements; >>>> E next() default #nextElement; >>>> void remove() default { throw new UnsupportedOperationException(); } >>>> } >>>> >>>> Note that Iterable is a function type -- a thunk producing an Iterator >> -- >>>> and so we can express Iterables with lambdas and method references. >> It's >>>> becoming clear that a for loop should provide a target type for these >>>> things, so that will probably be part of the SE 8 feature set. >>>> >>>> With a method reference: >>>> >>>> Hashtable<String,Object> h = …; >>>> for (String s : h#keys) { … } >>>> >>> >>> I'm struggling to see where this might be useful. This example is >> obviously >>> too trivial and would add nothing, but I can't see to come up with any >> more >>> extensive example where having a method reference in this position would >> be >>> actually useful. >>> >>> Can someone else provide a more compelling example for a for loop taking >> a >>> method reference? >>> >>> Thanks, >>> >>> Ben >>> >>> >> >> > AndyT (lordpixel - the cat who walks through walls) A little bigger on the inside (see you later space cowboy, you can't take the sky from me)