1. コンパイラプラグインのサポート

コンパイラプラグインのサポート 

コンパイラプラグインを使用するための特別なサポートがあります。この機能を有効にするには、autoCompilerPluginstrueに設定します。

autoCompilerPlugins := true

コンパイラプラグインを使用するには、アンマネージドライブラリディレクトリ(デフォルトではlib/)に配置するか、plugin設定でマネージド依存関係として追加します。addCompilerPluginは、依存関係の設定としてpluginを指定するための便利なメソッドです。

addCompilerPlugin("org.scala-tools.sxr" %% "sxr" % "0.3.0")

compileおよびtestCompileアクションは、libディレクトリまたはplugin設定にあるコンパイラプラグインを使用します。プラグインの必要な設定はユーザーの責任です。たとえば、Scala X-Rayは追加のオプションが必要です。

// declare the main Scala source directory as the base directory
scalacOptions :=
    scalacOptions.value :+ ("-Psxr:base-directory:" + (Compile / scalaSource).value.getAbsolutePath)

コンパイラプラグインを手動で指定することもできます。例:

scalacOptions += "-Xplugin:<path-to-sxr>/sxr-0.3.0.jar"

継続プラグインの例 

Scala 2.12での継続のサポートは、コンパイラプラグインとして実装されています。ここに示すように、このコンパイラプラグインサポートを使用できます。

val continuationsVersion = "1.0.3"

autoCompilerPlugins := true

addCompilerPlugin("org.scala-lang.plugins" % "scala-continuations-plugin_2.12.2" % continuationsVersion)

libraryDependencies += "org.scala-lang.plugins" %% "scala-continuations-library" % continuationsVersion

scalacOptions += "-P:continuations:enable"

バージョン固有のコンパイラプラグインの例 

バージョン固有のコンパイラプラグインの追加は、次のように行うことができます。

val continuationsVersion = "1.0.3"

autoCompilerPlugins := true

libraryDependencies +=
    compilerPlugin("org.scala-lang.plugins" % ("scala-continuations-plugin_" + scalaVersion.value) % continuationsVersion)

libraryDependencies += "org.scala-lang.plugins" %% "scala-continuations-library" % continuationsVersion

scalacOptions += "-P:continuations:enable"