AbstractTraversable.headOption throws NoSuchElementException
Rado Buranský <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <[email protected]> |
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.