Scala 2.8 breakOut

后端 未结 4 1033
无人及你
无人及你 2020-11-22 03:43

In Scala 2.8, there is an object in scala.collection.package.scala:

def breakOut[From, T, To](implicit b : CanBuildFrom[Nothing         


        
4条回答
  •  無奈伤痛
    2020-11-22 03:46

    A simple example to understand what breakOut does:

    scala> import collection.breakOut
    import collection.breakOut
    
    scala> val set = Set(1, 2, 3, 4)
    set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
    
    scala> set.map(_ % 2)
    res0: scala.collection.immutable.Set[Int] = Set(1, 0)
    
    scala> val seq:Seq[Int] = set.map(_ % 2)(breakOut)
    seq: Seq[Int] = Vector(1, 0, 1, 0) // map created a Seq[Int] instead of the default Set[Int]
    

提交回复
热议问题