我有以下scala(2.13.1)代码,该代码给我以下错误:
import scala.{Option => _, Either => _, _}
sealed trait Option[+A] {
def map[B](f: A => B): Option[B] = this match {
case None => None
case Some(_) => Some(f(_))
}
def flatMap[B](f: A => Option[B]): Option[B] = this match {
case None => None
case Some(_) => f(_)
}
def getOrElse[B >: A](default: => B): B = this match {
case None => default
case Some(x) => x
}
def orElse[B >: A](ob: => Option[B]): Option[B] = this match {
case None => ob
case _ => this
}
def filter(f: A => Boolean): Option[A] = this match {
case Some(a) if f(a) => this
case _ => None
}
def filter_1(f: A => Boolean): Option[A] =
flatMap(a => if (f(a)) Some(a) else None)
}
case class Some[+A](get: A) extends Option[A]
case object None extends Option[Nothing]
REPL错误:
scala> :load Option.scala
args: Array[String] = Array()
Loading Option.scala...
import scala.{Option=>_, Either=>_, _}
case None => None
^
Option.scala:3: error: pattern type is incompatible with expected type;
found : None.type
required: Option[A]
case None => None
^
Option.scala:3: error: type mismatch;
found : None.type
required: Option[B]
case Some(_) => Some(f(_))
^
Option.scala:4: error: constructor cannot be instantiated to expected type;
found : Some[A(in class Some)]
required: Option[A(in trait Option)]
case Some(_) => Some(f(_))
^
Option.scala:4: error: type mismatch;
found : Some[A => B]
required: Option[B]
case None => None
^
Option.scala:8: error: pattern type is incompatible with expected type;
found : None.type
required: Option[A]
case None => None
^
Option.scala:8: error: type mismatch;
found : None.type
required: Option[B]
case Some(_) => f(_)
^
Option.scala:9: error: constructor cannot be instantiated to expected type;
found : Some[A(in class Some)]
required: Option[A(in trait Option)]
case Some(_) => f(_)
^
Option.scala:9: error: type mismatch;
found : A => Option[B]
required: Option[B]
case None => default
^
Option.scala:13: error: pattern type is incompatible with expected type;
found : None.type
required: Option[A]
case Some(x) => x
^
Option.scala:14: error: constructor cannot be instantiated to expected type;
found : Some[A(in class Some)]
required: Option[A(in trait Option)]
case None => ob
^
Option.scala:18: error: pattern type is incompatible with expected type;
found : None.type
required: Option[A]
case Some(a) if f(a) => this
^
Option.scala:23: error: constructor cannot be instantiated to expected type;
found : Some[A(in class Some)]
required: Option[A(in trait Option)]
case _ => None
^
Option.scala:24: error: type mismatch;
found : None.type
required: Option[A]
flatMap(a => if (f(a)) Some(a) else None)
^
Option.scala:28: error: type mismatch;
found : Some[A]
required: Option[A]
flatMap(a => if (f(a)) Some(a) else None)
^
Option.scala:28: error: type mismatch;
found : None.type
required: Option[A]
case class Some[+A](get: A) extends Option[A]
^
Option.scala:1: error: illegal inheritance from sealed class Option
case object None extends Option[Nothing]
^
Option.scala:1: error: illegal inheritance from sealed class Option
在Scala中我在做什么错了?
答案 0 :(得分:3)
您应修复map
和flatMap
。将case Some(_) => ...
替换为case Some(x) => ...
,然后像在x
中一样使用此getOrElse
。
否则,Some(f(_))
,f(_)
是lambda而不是方法返回类型中承诺的Option
。
做
import scala.{Option => _, Some => _, None => _, Either => _, _}
在REPL中。
而不是:load Option.scala
使用:paste Option.scala
,而是将所有内容一起解释。