2016-03-31 5 views
3

をvalの定義:scoptは "見つかりません:値が" 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のいずれかの名前を変更する場合または値scriptsを入力してコンパイルします。

ここで起こっていることについて誰でも教えてもらえますか? コンパイラの問題ですか、それともいくつかの魔法がないのですか?

スカラ座2.11.8/SBT 0.13.7/scopt 3.5.0

答えて

3

代わりval秒のlazy val S内にヴァルスparserconfigscriptsを作ることは、より有用なエラーメッセージが得られます。/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に変更することです。

parserの定義を別のスコープに移動すると、名前変更や型名の回避が不要になるため、問題の根源はscoptライブラリのようです。

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") 
    } 
} 
関連する問題