scala typeclass注解本应被simulacrum删除,但实际上没有

yi0zb3m4  于 2023-02-22  发布在  Scala
关注(0)|答案(2)|浏览(145)

我正在尝试使用拟像编写一个简单的类型类。下面是我的build.sbt

ThisBuild / scalaVersion     := "2.12.8"
ThisBuild / version          := "0.1.0-SNAPSHOT"
ThisBuild / organization     := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
  .settings(
    name := "functional",
    autoCompilerPlugins := true,
    libraryDependencies ++= Seq(
      "com.github.mpilquist" %% "simulacrum" % "0.15.0",
      compilerPlugin("org.spire-math" %% "kind-projector" % "0.9.9")
    )
  )

这是我的代码

import simulacrum._

@typeclass trait Functor[F[_]] {
    def map[A, B](fa: F[A])(f: A => B) : F[B]
    def lift[A, B](fa: F[A])(f: A => B) : F[A] => F[B] = fa => map(fa)(f)
    def as[A, B](fa: F[A], b: => B) : F[B] = map(fa)(_ => b)
    def void[A](fa: F[A]) : F[Unit] = as(fa, ())
}

但我得到一个错误

typeclass annotation should have been removed by simulacrum but was not
[error] @typeclass trait Functor[F[_]] {
[error]                  ^
[error] one error found
4sup72z8

4sup72z81#

您没有添加宏天堂(Scala 2.11-2.12)

addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

https://github.com/mpilquist/simulacrum#including-simulacrum
在Scala 2.13中,只需打开scalacOptions += "-Ymacro-annotations"
Auto-Generate Companion Object for Case Class in Scala

0sgqnhkj

0sgqnhkj2#

//对于Scala 2.13+而不是addCompilerPlugin(...

scalacOptions += "-Ymacro-annotations"

相关问题