Type error when chaining map with toSet and using function literal with underscore

50 Views Asked by At

Passing function literal with underscore as an argument to map chained with toSet on another collection (e.g. List) results in type error:

scala> List(1, 2, 3).toSet map (_.toString)
<console>:12: error: missing parameter type for expanded function ((x$1) => x$1.toString)
   List(1, 2, 3).toSet map (_.toString)

However, when Set produced in the same manner is assigned to a value, subsequent invocation of map works as expected:

scala> val set = List(1, 2, 3).toSet
set: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

scala> set map (_.toString)
res1: scala.collection.immutable.Set[String] = Set(1, 2, 3)

Back to chaining, when constructor is used to produce Set from another collection (e.g. List) it works as expected:

scala> Set(List(1, 2, 3):_*) map (_.toString)
res1: scala.collection.immutable.Set[String] = Set(1, 2, 3)

Is this a bug in Scala compiler or is there something else that I'm missing?

0

There are 0 best solutions below