Skip to content

Least upper bound in List.foreach[U](f: A => U) #11769

Closed
@kamilkloch

Description

Not sure if this is a bug or an expected behavior.
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?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions