什么不编译?

时间:2017-03-17 11:13:08

标签: scala

我有一个特点如下:

sealed trait Option[+A] {

  def map[B](f: A => B): Option[B] = this match {
    case None => None
    case Some(a) => Some(f(a))
  }

}

case class Some[+A](get: A) extends Option[A]
case object None extends Option[Nothing]

当我尝试编译时,我遇到以下错误:

scala> :load "src/Option.scala"
Loading src/Option.scala...
src/Option.scala:17: error: pattern type is incompatible with expected type;
 found   : None.type
 required: Option[A]
           case None => None
                ^
src/Option.scala:17: error: type mismatch;
 found   : None.type
 required: Option[B]
           case None => None
                        ^
src/Option.scala:18: error: constructor cannot be instantiated to expected type;
 found   : Some[A(in class Some)]
 required: Option[A(in trait Option)]
           case Some(a) => Some(f(a))
                ^
src/Option.scala:18: error: type mismatch;
 found   : Some[B]
 required: Option[B]
           case Some(a) => Some(f(a))
                               ^
src/Option.scala:11: error: illegal inheritance from sealed class Option
       case class Some[+A](get: A) extends Option[A]
                                           ^
src/Option.scala:11: error: illegal inheritance from sealed class Option
       case object None extends Option[Nothing]

为什么呢?

我是scala世界的新手,如果map是函数或方法,则无法区分?我该如何使用地图?

0 个答案:

没有答案