加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 为什么“set”无法为自定义SettingKey赋值?我可以在sb

发布时间:2020-12-16 21:33:25 所属栏目:安全 来源:网络整理
导读:以下是具有自定义设置的sbt 0.13.1项目及其值: % pwd/Users/tisue/myproj% lsbuild.sbt% cat build.sbtval foo = settingKey[String]("This is a custom setting")foo := "bar"% sbt[info] Set current project to myproj (in build file:/Users/tisue/mypr
以下是具有自定义设置的sbt 0.13.1项目及其值:

% pwd
/Users/tisue/myproj
% ls
build.sbt
% cat build.sbt
val foo = settingKey[String]("This is a custom setting")

foo := "bar"
% sbt
[info] Set current project to myproj (in build file:/Users/tisue/myproj/)
> show foo
[info] bar

到现在为止还挺好.但现在:

> set foo := "qux"
<set>:1: error: not found: value foo
foo := "qux"
^
[error] Type error in expression

这不行吗

我部分地了解这里出了什么问题集合评估Scala表达式,并且该表达式显然是在val foo不在范围内的上下文中编译的.

但是,当我们从.sbt文件中编译foo:= …时,我会期待使foo的范围成为可能,当shell中编译相同的东西时,它也会生效.

解决方法

截至0.13.6(2014-09-12),这不再是限制( #1059/ #1456)

原始答案 – 任何使用sbt 0.13.0 – 0.13.5的项目

事实证明0.13.0 changes清楚地表明这是一个预期的行为:

All definitions are compiled before settings,but it will probably be best practice to put definitions together. Currently,the visibility of definitions is restricted to the .sbt file it is defined in. They are not visible in consoleProject or the set command at this time,either. Use Scala files in project/ for visibility in all .sbt files.

这样,您应该可以在项目/ Build.scala中定义之后共享该设置,如下所示:

import sbt._
import Keys._

object HelloBuild extends Build {
  lazy val foo = settingKey[String]("This is a custom setting")

  foo := "Build.scala"
}

build.sbt

scalaVersion := "2.10.4-RC1"

foo := "build.sbt"

然后,在sbt shell中:

[sbt-0-13-1]> foo
[info] build.sbt
[sbt-0-13-1]> set foo := "shell"
[info] Defining *:foo
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to sbt-0-13-1 (in build file:/Users/jacek/sandbox/so/sbt-0.13.1/)
[sbt-0-13-1]> foo
[info] shell

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读