【问题标题】:scopt "not found: value" defining val with same namescopt“未找到:值”定义具有相同名称的 val
【发布时间】:2016-03-31 10:47:18
【问题描述】:

对于 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

【问题讨论】:

    标签: scala sbt scopt


    【解决方案1】:

    将 vals parserconfigscripts 改为 lazy vals 而不是 vals 会提供更有用的错误消息:/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 scriptscase 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")
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多