Possible bug in JavaConverters Set iteration

After updating to Scala 2.13 I came across some unexpected behavior in iteration over a Java set using JavaConverters. I had a set of size 1 which appeared to result in two “loops” within a for comprehension. You can see this by performing a side effect when mapping over the set:

Welcome to Scala 2.13.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_141).
Type in expressions for evaluation. Or try :help.

scala> import scala.collection.JavaConverters._
import scala.collection.JavaConverters._

scala> import java.util
import java.util

scala> val jSet = new util.HashSet[String]()
jSet: java.util.HashSet[String] = []

scala> jSet.add("hello")
res0: Boolean = true

scala> jSet.asScala.map(println)
warning: there was one deprecation warning (since 2.13.0); for details, enable `:setting -deprecation' or `:replay -deprecation'
res1: scala.collection.mutable.Set[Unit] = HashSet(())

I know it is unexpected to perform side effects within .map, and the returned set is of the correct size, but should this be considered a bug?

Confirmed !! … it happens to me too, and not only with print, but with any function -> it’s called twice !!

I’ve opened an Scala issue at -> https://github.com/scala/bug/issues/11629

Excellent, thanks for confirming.

1 Like

I submit a pr,
welcome to review :smile:
link : https://github.com/scala/scala/pull/8218

1 Like

Thanks redscarf for the PR !!