![](/img/trans.png)
[英]Scala, cats - how to create tagless-final implementation with IO (or other monad) and Either?
[英]Cats-tagless - Scala macro annotation error
我使用cats-tagless
lib創建了一個簡單的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]
}
如此處廣告所示,編譯就可以了 。
如果刪除addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M11" cross CrossVersion.full)
, addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M11" cross CrossVersion.full)
收到相同的錯誤消息:
宏注釋無法擴展(最常見的原因是您需要啟用宏天堂插件;另一種可能性是您嘗試在定義宏注釋的同一編譯運行中使用宏注釋)
同樣,正如所記錄的那樣,鏈接頁面顯示:
宏注釋(@ finalAlg,
@autoFunctorK
,@ autoInvariantK等)仍然取決於scalameta,因此您需要在build.sbt中添加scalameta依賴項。
因此似乎您需要它,因為@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
子項目中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.