当试图在 Scala 中实现optionMonoid[A]
from FP 时,我写道:
def optionMonoid[A] = new Monoid[Option[A]] {
def op(a1: Option[A], a2: Option[A]) = (a1, a2) match {
case (Some(x), Some(y)) => Some(x + y)
case _ => None
}
val zero = None
}
为什么编译时错误在这里需要一个字符串?
C:\Users\Kevin\Workspace\side-work\Monoid>scalac MonoidTesting.scala
MonoidTesting.scala:43: error: type mismatch;
found : A
required: String
case (Some(x), Some(y)) => Some(x + y)
^