如何将 scala case class 声明为 Scalaz 的 Semigroup 的实例?

How do I declare a scala case class to be an instance of Scalaz's Semigroup?

我有一个类型,定义如下:

import scalaz._, Scalaz._
case class MyInt(i : Int)

我想制作 Semigroup 的实例。我试过这个:

object MyInt {
  implicit def myIntSemigroup: Semigroup[MyInt] = new Semigroup[MyInt] {                                                                                                                                                                       
    def append(a: MyInt, b: MyInt) : MyInt = MyInt(a.i + b.i)
  }
}

当我 运行 sbt console 时,我得到这个错误:

[info] Set current project to hello (in build file:/home/mp/code/scala/examples/semigroup/)
[info] Compiling 1 Scala source to /home/mp/code/scala/examples/semigroup/target/scala-2.10/classes...
[error] /home/mp/code/scala/examples/semigroup/src/main/scala/Main.scala:6: object creation impossible, since method append in trait Semigroup of type (f1: MyInt, f2: => MyInt)MyInt is not defined
[error]   implicit def myIntSemigroup: Semigroup[MyInt] = new Semigroup[MyInt] {                                                                                                                                                               
[error]                                                       ^   
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 5 s, completed 25-Jun-2015 08:15:37

如何将 MyInt 变成 Semigroup 以便我可以在上面使用 |+|

为了完整起见,这是我的 build.sbt 文件:

name := "hello"
version := "1.0"
scalaVersion := "2.10.5"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "7.1.3"

如果您仔细查看 Scalaz 半群的签名,第二个参数是按名称定义的,而在您的实现中您有不同的签名。将第二个参数更改为 b: => MyInt,它应该编译