Re: AbstractTraversable.headOption throws NoSuchElementException
Nils Kilden-Pedersen <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CABDULvWBsEsx5L8DxA2CxrfuhGQKjCiDh-+wgrt5GbZKvt=EgQ@mail.gmail.com> |
A java.lang.Iterable should return a valid Iterator when requested. Any implementation that doesn’t do that, is either faulty, or designed that way thus not generally usable. On Wed, May 6, 2015 at 2:25 PM, Rado Buranský <[email protected]> wrote: > The subject is not really correct, but I'd like to know you're opinion on > how to avoid this kind of issue: > > import scala.collection.JavaConversions._ > > val files: java.lang.Iterable[...] = ... > files.headOption match { ... // This throws NoSuchElementException > > If you're curious the stack trace looked like this: > ... > Caused by: java.util.NoSuchElementException > at > com.google.common.collect.AbstractIterator.next(AbstractIterator.java:152) > at > scala.collection.convert.Wrappers$JIteratorWrapper.next(Wrappers.scala:42) > at scala.collection.IterableLike$class.head(IterableLike.scala:91) > at scala.collection.AbstractIterable.head(Iterable.scala:54) > at > scala.collection.TraversableLike$class.headOption(TraversableLike.scala:436) > at scala.collection.AbstractTraversable.headOption(Traversable.scala:105) > ... > > The reason is that the implementation of the Iterable can be iterated only > once. But it took me a while to find it out. I simply called an API > (SonarQube) to get a collection of files and I naturally didn't care about > implementation of the Iterable interface. Should I? Who's fault is this and > how to avoid it? > > Thanks for opinions > > -- > You received this message because you are subscribed to the Google Groups > "scala-language" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "scala-language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.