巨集

巨集天堂

語言
此文件頁面專門針對 Scala 2 中發布的功能,這些功能已在 Scala 3 中移除或由其他功能取代。除非另有說明,否則此頁面中的所有程式碼範例都假設您使用的是 Scala 2。

最新

Eugene Burmako

我一直想像天堂會是一種圖書館。豪爾赫·路易斯·博爾赫斯,「禮物之詩」

Macro paradise 是 Scala 編譯器各種版本的附加元件。它設計為可與 `scalac` 的生產版本可靠地搭配使用,讓最新的巨集開發在未來版本的 Scala 中推出之前就能使用。請參閱 支援功能和版本清單 的路線圖,並瀏覽 paradise 公告,以進一步了解我們的支援保證。

~/210x $ scalac -Xplugin:paradise_*.jar -Xshow-phases
    phase name  id  description
    ----------  --  -----------
        parser   1  parse source into ASTs, perform simple desugaring
 macroparadise   2  let our powers combine
         namer   3  resolve names, attach symbols to trees in paradise
packageobjects   4  load package objects in paradise
         typer   5  the meat and potatoes: type the trees in paradise
                ...

macro paradise 中的一些功能會帶來對 macro paradise 附加元件的編譯時間相依性,有些功能則不會,不過這些功能在執行階段都不需要 macro paradise。請繼續瀏覽 功能清單 文件,以取得更多資訊。

請參閱 https://github.com/scalamacros/sbt-example-paradise,以取得端對端的範例,但簡單來說,使用 macro paradise 就像在您的建置中新增下列兩行一樣容易(假設您已 設定 sbt 以使用巨集)。

resolvers += Resolver.sonatypeRepo("releases")
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

若要在 Maven 中使用 macro paradise,請依照 Stack Overflow 在頁面 “在 Maven 專案中啟用 macro-paradise Scala 編譯器附加元件” 上提供的指示(也請務必新增對 Sonatype 快照儲存庫和 scala-reflect.jar 的相依性)。

<compilerPlugins>
  <compilerPlugin>
    <groupId>org.scalamacros</groupId>
    <artifactId>paradise_<YOUR.SCALA.VERSION></artifactId>
    <version>2.1.0</version>
  </compilerPlugin>
</compilerPlugins>

macro paradise 的來源可從 https://github.com/scalamacros/paradise 取得。有支援最新 2.10.x 版本、最新 2.11.x 版本、2.10.x、2.11.x 和 2.12.x 快照,以及 Scala 虛擬化。

此頁面的貢獻者