以下两个命令创建一个Set[Char]
,然后根据需要map
-ing 给出一个Set[String]
:
scala> ('a' to 'z').toSet
res15: scala.collection.immutable.Set[Char] = Set(e, s, ...)
scala> res15.map(_.toString)
res16: scala.collection.immutable.Set[String] = Set(e, s, ...)
但是为什么以下方法不起作用呢?
scala> ('a' to 'z').toSet.map(_.toString)
<console>:12: error: missing parameter type for expanded
function ((x$1) => x$1.toString)('a' to 'z').toSet.map(_.toString)
这个替代方案非常有效:
('a' to 'z').toSet[Char].map(_.toString)
发生这种情况是因为toSet
接受[B >: A]
类型参数,并且无法直接推断要调用的类型toString