Cats-tagless - Scala 宏注释错误
Cats-tagless - Scala macro annotation error
我使用 cats-tagless
库创建了一个简单的 trait
:
@finalAlg
@autoFunctorK(true)
trait MyService[F[_]] {
def put(element: Element): F[Element]
def get(elementId: Id): F[Element]
def all(): F[List[Element]]
def delete(elementId: Id): F[Unit]
}
但是当我尝试编译它时,出现错误:
Error:(8, 7) macro annotation could not be expanded (the most common reason for that is that you need to enable the macro paradise plugin; another possibility is that you try to use macro annotation in the same compilation run that defines it)
我还将 addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full)
添加到 plugins.sbt
文件和 build.sbt
中,但没有帮助。你能帮我解决吗?
我的 build.sbt
文件如下所示:
addCompilerPlugin(("org.scalameta" % "paradise" % "3.0.0-M11").cross(CrossVersion.full))
lazy val commonSettings = Seq(
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % CatsVersion,
"org.typelevel" %% "cats-effect" % "1.2.0",
"org.typelevel" %% "cats-tagless-macros" % "0.5",
"org.typelevel" %% "cats-tagless-legacy-macros" % "0.5",
"org.typelevel" %% "cats-mtl-core" % "0.5.0",
)
)
在一个空的新项目中 build.sbt
:
scalaVersion := "2.12.8"
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-tagless-macros" % "0.5",
"org.typelevel" %% "cats-tagless-legacy-macros" % "0.5"
)
addCompilerPlugin(
"org.scalameta" % "paradise" % "3.0.0-M11" cross CrossVersion.full
)
此代码:
import cats.tagless._
case class Element()
case class Id()
@finalAlg
@autoFunctorK(true)
trait MyService[F[_]] {
def put(element: Element): F[Element]
def get(elementId: Id): F[Element]
def all(): F[List[Element]]
def delete(elementId: Id): F[Unit]
}
编译得很好,正如所宣传的那样here。
如果我删除 addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M11" cross CrossVersion.full)
,我会收到相同的错误消息:
macro annotation could not be expanded (the most common reason for that is that you need to enable the macro paradise plugin; another possibility is that you try to use macro annotation in the same compilation run that defines it)
同样,这是记录在案的,链接页面说:
The macro annotations (@finalAlg, @autoFunctorK
, @autoInvariantK, etc.) still depends on scalameta, so you need to add scalameta dependencies in build.sbt.
看来你需要它是因为 @finalAlg
和 @autoFunctorK
。
请注意,我没有修改 project/
中的任何内容。
编辑
如果您有多个子项目,您必须将编译器插件添加到实际需要它的子项目中。请注意
addCompilerPlugin(foobar)
本质上只是
libraryDependencies += compilerPlugin(foobar)
所以在你的情况下,你可能应该尝试像
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % "1.6.0",
"org.typelevel" %% "cats-effect" % "1.2.0",
"org.typelevel" %% "cats-tagless-macros" % "0.5",
"org.typelevel" %% "cats-tagless-legacy-macros" % "0.5",
"org.typelevel" %% "cats-mtl-core" % "0.5.0",
compilerPlugin(("org.scalameta" % "paradise" % "3.0.0-M11")
.cross(CrossVersion.full))
)
然后将其添加到您的 algebra
子项目中。
我使用 cats-tagless
库创建了一个简单的 trait
:
@finalAlg
@autoFunctorK(true)
trait MyService[F[_]] {
def put(element: Element): F[Element]
def get(elementId: Id): F[Element]
def all(): F[List[Element]]
def delete(elementId: Id): F[Unit]
}
但是当我尝试编译它时,出现错误:
Error:(8, 7) macro annotation could not be expanded (the most common reason for that is that you need to enable the macro paradise plugin; another possibility is that you try to use macro annotation in the same compilation run that defines it)
我还将 addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full)
添加到 plugins.sbt
文件和 build.sbt
中,但没有帮助。你能帮我解决吗?
我的 build.sbt
文件如下所示:
addCompilerPlugin(("org.scalameta" % "paradise" % "3.0.0-M11").cross(CrossVersion.full))
lazy val commonSettings = Seq(
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % CatsVersion,
"org.typelevel" %% "cats-effect" % "1.2.0",
"org.typelevel" %% "cats-tagless-macros" % "0.5",
"org.typelevel" %% "cats-tagless-legacy-macros" % "0.5",
"org.typelevel" %% "cats-mtl-core" % "0.5.0",
)
)
在一个空的新项目中 build.sbt
:
scalaVersion := "2.12.8"
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-tagless-macros" % "0.5",
"org.typelevel" %% "cats-tagless-legacy-macros" % "0.5"
)
addCompilerPlugin(
"org.scalameta" % "paradise" % "3.0.0-M11" cross CrossVersion.full
)
此代码:
import cats.tagless._
case class Element()
case class Id()
@finalAlg
@autoFunctorK(true)
trait MyService[F[_]] {
def put(element: Element): F[Element]
def get(elementId: Id): F[Element]
def all(): F[List[Element]]
def delete(elementId: Id): F[Unit]
}
编译得很好,正如所宣传的那样here。
如果我删除 addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M11" cross CrossVersion.full)
,我会收到相同的错误消息:
macro annotation could not be expanded (the most common reason for that is that you need to enable the macro paradise plugin; another possibility is that you try to use macro annotation in the same compilation run that defines it)
同样,这是记录在案的,链接页面说:
The macro annotations (@finalAlg,
@autoFunctorK
, @autoInvariantK, etc.) still depends on scalameta, so you need to add scalameta dependencies in build.sbt.
看来你需要它是因为 @finalAlg
和 @autoFunctorK
。
请注意,我没有修改 project/
中的任何内容。
编辑
如果您有多个子项目,您必须将编译器插件添加到实际需要它的子项目中。请注意
addCompilerPlugin(foobar)
本质上只是
libraryDependencies += compilerPlugin(foobar)
所以在你的情况下,你可能应该尝试像
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-core" % "1.6.0",
"org.typelevel" %% "cats-effect" % "1.2.0",
"org.typelevel" %% "cats-tagless-macros" % "0.5",
"org.typelevel" %% "cats-tagless-legacy-macros" % "0.5",
"org.typelevel" %% "cats-mtl-core" % "0.5.0",
compilerPlugin(("org.scalameta" % "paradise" % "3.0.0-M11")
.cross(CrossVersion.full))
)
然后将其添加到您的 algebra
子项目中。