Re: AbstractTraversable.headOption throws NoSuchElementException

Rex Kerr <[email protected]>
Newsgroups gmane.comp.lang.scala
Message-ID <CAP_xLa3mS4rqWOx2gHQvxDY5sc82RPhiknbE=E-ZGCaz=FULqA@mail.gmail.com>
For what it's worth, there's no reason why headOption should traverse
twice.  I think I have a performance optimization patch somewhere in the
works that will fix this.  So then _this_ code won't break, but other code
that should work will break because this java.lang.Iterable only returns
one good Iterator, which isn't really what one expects from an Iterable.
If it's only good once, it should just be an Iterator.

I'm not sure there's any way to catch wonky usages of the API except to try
them and see what happens (and submit a bug report to the offending project
if it does something weird).

  --Rex

On Wed, May 6, 2015 at 12:34 PM, Nils Kilden-Pedersen <[email protected]>
wrote:

> 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.
>

-- 
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.