scopt "not found: value" 定义具有相同名称的 val
scopt "not found: value" defining val with same name
scopt 的以下用法:
import java.io.File
object Application extends App {
case class Config(in: File = new File("."), out: File = new File("."), scripts: Seq[File] = Seq())
val parser = new scopt.OptionParser[Config]("scopt") {
head("Script Runner", "0.1")
opt[File]('i', "in") required () valueName ("<path>") action { (x, c) =>
c.copy(in = x)
} text ("required in path property")
opt[File]('o', "out") required () valueName ("<path>") action { (x, c) =>
c.copy(out = x)
} text ("required out path file property")
arg[File]("<file>...") unbounded () required() action { (x, c) =>
c.copy(scripts = c.scripts :+ x)
} text ("unbounded script paths")
}
val config = parser.parse(args, Config())
val scripts = config.map { argConfig => argConfig.scripts }
config match {
case Some(config) => println(config)
case _ => println("config undefined")
}
}
我得到编译错误:
[error] /Users/jamesclark/code/scratch/src/main/scala/Application.scala:13: not found: value x
[error] c.copy(out = x)
如果我重命名 Config
参数 scripts
或 val scripts
然后它编译。
谁能告诉我这里发生了什么?
是编译器问题,还是我遗漏了一些魔法?
scala 2.11.8/sbt 0.13.7/scopt 3.5.0
将 vals parser
、config
和 scripts
设为 lazy val
s 而不是 val
s 会给出更有用的错误消息:/src/main/scala/Application.scala:23: variable definition needs type because 'scripts' is used as a named argument in its body.
为脚本提供类型注释 val scripts: Option[Seq[File]] = ...
解决了问题。
解决此问题的替代方法是重命名 val scripts
或 case class Config(... scripts: Seq[File] ...)
出现的 scripts
。
问题的根源似乎来自 scopt 库,因为将 parser
的定义移动到一个单独的范围内就不需要任何重命名或类型注释解决方法。
object Application extends App {
def newParser(): OptionParser[Config] = {
new scopt.OptionParser[Config]("scopt") {
head("Script Runner", "0.1")
opt[File]('i', "in") required () valueName ("<path>") action { (x, c) =>
c.copy(in = x)
} text ("required in path property")
opt[File]('o', "out") required () valueName ("<path>") action { (x, c) =>
c.copy(out = x)
} text ("required out path file property")
arg[File]("<file>...") unbounded () required() action { (x, c) =>
c.copy(scripts = c.scripts :+ x)
} text ("unbounded script paths")
}
}
case class Config(in: File = new File("."), out: File = new File("."), scripts: Seq[File] = Seq())
val parser = newParser()
val config = parser.parse(args, Config())
val scripts = config.map { argConfig => argConfig.scripts }
config match {
case Some(config) =>
println(config)
case _ =>
println("config undefined")
}
}
scopt 的以下用法:
import java.io.File
object Application extends App {
case class Config(in: File = new File("."), out: File = new File("."), scripts: Seq[File] = Seq())
val parser = new scopt.OptionParser[Config]("scopt") {
head("Script Runner", "0.1")
opt[File]('i', "in") required () valueName ("<path>") action { (x, c) =>
c.copy(in = x)
} text ("required in path property")
opt[File]('o', "out") required () valueName ("<path>") action { (x, c) =>
c.copy(out = x)
} text ("required out path file property")
arg[File]("<file>...") unbounded () required() action { (x, c) =>
c.copy(scripts = c.scripts :+ x)
} text ("unbounded script paths")
}
val config = parser.parse(args, Config())
val scripts = config.map { argConfig => argConfig.scripts }
config match {
case Some(config) => println(config)
case _ => println("config undefined")
}
}
我得到编译错误:
[error] /Users/jamesclark/code/scratch/src/main/scala/Application.scala:13: not found: value x
[error] c.copy(out = x)
如果我重命名 Config
参数 scripts
或 val scripts
然后它编译。
谁能告诉我这里发生了什么? 是编译器问题,还是我遗漏了一些魔法?
scala 2.11.8/sbt 0.13.7/scopt 3.5.0
将 vals parser
、config
和 scripts
设为 lazy val
s 而不是 val
s 会给出更有用的错误消息:/src/main/scala/Application.scala:23: variable definition needs type because 'scripts' is used as a named argument in its body.
为脚本提供类型注释 val scripts: Option[Seq[File]] = ...
解决了问题。
解决此问题的替代方法是重命名 val scripts
或 case class Config(... scripts: Seq[File] ...)
出现的 scripts
。
问题的根源似乎来自 scopt 库,因为将 parser
的定义移动到一个单独的范围内就不需要任何重命名或类型注释解决方法。
object Application extends App {
def newParser(): OptionParser[Config] = {
new scopt.OptionParser[Config]("scopt") {
head("Script Runner", "0.1")
opt[File]('i', "in") required () valueName ("<path>") action { (x, c) =>
c.copy(in = x)
} text ("required in path property")
opt[File]('o', "out") required () valueName ("<path>") action { (x, c) =>
c.copy(out = x)
} text ("required out path file property")
arg[File]("<file>...") unbounded () required() action { (x, c) =>
c.copy(scripts = c.scripts :+ x)
} text ("unbounded script paths")
}
}
case class Config(in: File = new File("."), out: File = new File("."), scripts: Seq[File] = Seq())
val parser = newParser()
val config = parser.parse(args, Config())
val scripts = config.map { argConfig => argConfig.scripts }
config match {
case Some(config) =>
println(config)
case _ =>
println("config undefined")
}
}