相关疑难解决方法(0)

Scala 2.8 breakOut

在Scala 2.8中,有一个对象scala.collection.package.scala:

def breakOut[From, T, To](implicit b : CanBuildFrom[Nothing, T, To]) =
    new CanBuildFrom[From, T, To] {
        def apply(from: From) = b.apply() ; def apply() = b.apply()
 }
Run Code Online (Sandbox Code Playgroud)

我被告知这会导致:

> import scala.collection.breakOut
> val map : Map[Int,String] = List("London", "Paris").map(x => (x.length, x))(breakOut)

map: Map[Int,String] = Map(6 -> London, 5 -> Paris)
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?为什么breakOut被称为我的论据List

scala scala-2.8 scala-collections

223
推荐指数
4
解决办法
3万
查看次数

在Scala中,使用`_`和使用命名标识符有什么区别?

当我尝试使用_而不是使用命名标识符时,为什么会出现错误?

scala> res0
res25: List[Int] = List(1, 2, 3, 4, 5)

scala> res0.map(_=>"item "+_.toString)
<console>:6: error: missing parameter type for expanded function ((x$2) => "item
 ".$plus(x$2.toString))
       res0.map(_=>"item "+_.toString)
                           ^

scala> res0.map(i=>"item "+i.toString)
res29: List[java.lang.String] = List(item 1, item 2, item 3, item 4, item 5)
Run Code Online (Sandbox Code Playgroud)

scala wildcard

10
推荐指数
2
解决办法
5139
查看次数

标签 统计

scala ×2

scala-2.8 ×1

scala-collections ×1

wildcard ×1