什么都没有

时间:2014-10-11 23:31:32

标签: scala types nothing

A在此过程中如何变成Nothing

def seq2map[A](src: Seq[A]): Map[A, A] = {
    def pair = for {
        f <- src.headOption
        s <- src.headOption
    } yield (f, s)
    Stream continually pair takeWhile(_ isDefined) toMap
}

错误:类型Map[Nothing, Nothing]的表达式不符合预期类型Map[A, A]

谢谢!

1 个答案:

答案 0 :(得分:4)

我得到了

<console>:12: error: Cannot prove that Option[(A, A)] <:< (T, U).
           Stream continually pair takeWhile(_ isDefined) toMap
                                                          ^

,因为

scala> val src = (1 to 10).toSeq
src: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala>     def pair = for {
     |         f <- src.headOption
     |         s <- src.headOption
     |     } yield (f, s)
pair: Option[(Int, Int)]

不是一对,而是一个选项。

scala> (Stream continually pair takeWhile (_.isDefined)).flatten
res0: scala.collection.immutable.Stream[(Int, Int)] = Stream((1,1), ?)

是一对配对。

等待游戏开始。