Consider the following, which compiles:
val f: String => Set[Integer] = ???
val a: Set[String] = ???
val b = a.flatMap(s => f(s))
Now, if I change the first line above as following, the code no longer compiles:
val f: String => Set[_ <: Integer] = ???
val a: Set[String] = ???
val b = a.flatMap(s => f(s))
The error is the following:
/Foo.scala:31: error: no type parameters for method flatMap: (f: String => scala.collection.GenTraversableOnce[B])(implicit bf: scala.collection.generic.CanBuildFrom[scala.collection.immutable.Set[String],B,That])That exist so that it can be applied to arguments (String => scala.collection.immutable.Set[_ <: Integer])
[ERROR] --- because ---
[ERROR] argument expression's type is not compatible with formal parameter type;
[ERROR] found : String => scala.collection.immutable.Set[_ <: Integer]
[ERROR] required: String => scala.collection.GenTraversableOnce[?B]
[ERROR] val b = a.flatMap(s => f(s))
[ERROR] ^
[ERROR] /Foo.scala:31: error: type mismatch;
[ERROR] found : String => scala.collection.immutable.Set[_ <: Integer]
[ERROR] required: String => scala.collection.GenTraversableOnce[B]
[ERROR] val b = a.flatMap(s => f(s))
[ERROR] ^
[ERROR] /Foo.scala:31: error: Cannot construct a collection of type That with elements of type B based on a collection of type scala.collection.immutable.Set[String].
[ERROR] val b = a.flatMap(s => f(s))
Why does a compile error result here (I don't understand the above compiler error message), and how should I fix it?