【发布时间】:2013-10-20 22:02:46
【问题描述】:
看起来很奇怪,但这不起作用:
scala> (1 to 6).toSet map (_ / 2)
<console>:8: error: missing parameter type for expanded function ((x$1) => x$1.$div(2))
(1 to 6).toSet map (_ / 2)
^
但是,使用 to[Set] 而不是 toSet 会:
scala> (1 to 6).to[Set] map (_ / 2)
res0: scala.collection.immutable.Set[Int] = Set(2, 0, 3, 1)
嗯。 o_O
还认为这是可行的:
scala> val s = (1 to 6).toSet; s map (_ / 2)
s: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 3, 4)
res1: scala.collection.immutable.Set[Int] = Set(2, 0, 3, 1)
正如@AlexIv 所建议的那样,Range.Inclusive 是一阶类型,请记住,这也不适用于List[Int]:
scala> List(1, 2, 3, 4, 5, 6).toSet map (_ / 2)
<console>:8: error: missing parameter type for expanded function ((x$1) => x$1.$
div(2))
List(1, 2, 3, 4, 5, 6).toSet map (_ / 2)
^
和以前一样,这是可行的:
scala> val s = List[Int](1, 2, 3, 4, 5, 6).toSet; s map (_ / 2)
s: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 3, 4)
res3: scala.collection.immutable.Set[Int] = Set(2, 0, 3, 1)
【问题讨论】:
-
@som-snytt,非常感谢!
标签: scala type-inference scala-collections