我读过一篇reference了解范围委托(delegate)在 SBT 中的工作原理。
从上面的引用页中,我摘录了练习 D。
ThisBuild / scalacOptions += "-Ywarn-unused-import"
lazy val projD = (project in file("d"))
.settings(
test := {
println((Compile / console / scalacOptions).value)
},
console / scalacOptions -= "-Ywarn-unused-import",
Compile / scalacOptions := scalacOptions.value // added by sbt
)
What would you see if you ran projD/test?
List()
List (-Ywarn-unused-import)
something else?
他们的推理是
The answer is List(-Ywarn-unused-import). Rule 2 finds projD / Compile / Zero, Rule 3 finds projD / Zero / console, and Rule 4 finds ThisBuild / Zero / Zero. Rule 1 selects projD / Compile / Zero because it has the subproject axis projD, and the configuration axis has higher precedence over the task axis.
到目前为止,我可以理解为什么 Compile/console/sclacOptions 的范围将限定为 projD/Compile/zero/scalacOptions。这是因为与我们想知道的键(Compile/console/scalacOptions)相比,projD/Compile/zero 的配置轴比 projD/zero/console 更具体。
Next, Compile / scalacOptions refers to scalacOptions.value, we next need to find a delegate for projD / Zero / Zero. Rule 4 finds ThisBuild / Zero / Zero and thus it resolves to List(-Ywarn-unused-import).
在这里,我无法理解为什么 ThisBuild/Zero/Zero 赢得了 projD/Compile/Zero。因为我们要查找的键的范围是 projD/zero/zero,所以 projD/Compile/zero 相比 Thisbuild [遵循引用中的规则 1] 具有更具体的值。
我认为原因是Compile/scalacOptions具有scalacOptions.value的值,它生成递归定义。然后,我们可以使用 projD/Zero/控制台。
最佳答案
我认为您只是对解释第二部分中对差异范围的引用有点困惑。让我们明确地写下示例中的所有范围:
ThisBuild / Zero / Zero / scalacOptions += "-Ywarn-unused-import"
lazy val projD = (project in file("d"))
.settings(
test := { println((Compile / console / scalacOptions).value) }
)
projD / Zero / console / scalacOptions -= "-Ywarn-unused-import"
projD / Compile / Zero / scalacOptions := (projD / Zero / Zero / scalacOptions).value
我必须删除这两个设置,才能引用 projD
,但除此之外它的定义相同。
因此,从第一部分开始,您就了解到 projD/Compile/Zero/scalacOptions
胜过 projD/Zero/console/scalacOptions
和 ThisBuild/...
。
现在让我们看看它的值(value)并在我们的任务中使用。但它的值(value)是什么?它指的是未明确定义的projD/Zero/Zero/scalacOptions
。所以我们需要找到一个代表。目前,我们已经不再与 projD/Compile
和 projD/console
竞争,范围内唯一合适的键位于 ThisBuild/Zero/Zero 范围内
.
我试图在这里说明它(省略 /scalacOptions
以节省空间):
projD / Compile / console // the one we need in the task
projD / Zero / console // could be applied by rule 3, but looses to the next one:
projD / Compile / Zero // applies by rule 2 (task scope delegation), defined with next one:
projD / Zero / Zero // not defined explicitly, so is delegated:
ThisBuild / Zero / Zero // applies by rule 4 (project scope delegation)
关于scala - <scala> 范围委托(delegate)在 SBT 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49066303/