Scala inferred least upper bound

Viewed 53

Scala 2.12. The following code

val mapBuilder = Map.newBuilder[String, String]
val setBuilder = Set.newBuilder[String]

List(Option("")).foreach {
  case Some(_) => mapBuilder += "" -> ""
  case None => setBuilder += ""
}

does not compile - compiler fails to infer the least upper bound of the two builders (the U in foreach[U](f: A => U)):

type arguments [String,Iterable[java.io.Serializable] with String => Any with scala.collection.generic.Subtractable[String,Equals]] do not conform to trait Subtractable's type parameter bounds [A,+Repr <: scala.collection.generic.Subtractable[A,Repr]]

Annotating the foreach with Any resolves the problem:

List(Option("")).foreach[Any] {
...

Is this an expected behavior?

0 Answers
Related