6

まず、これは Scala 2.8 にあるので、そこにあるはずです! =)

私はLiftのJavascriptオブジェクトに取り組んでおり、次のものが必要です:

case class JsVar(varName: String, andThen: String*) extends JsExp {
  // ...
  def -&(right: String) = copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
}

残念ながら、次のコンパイラ エラーが発生します。

[error] Lift/framework/web/webkit/src/main/scala/net/liftweb/http/js/JsCommands.scala:452: not found: value copy
[error]     def -&(right: String) = copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
[error]

ケースクラスにはプロパティがあるので、copyメソッドがあるはずですよね?

試してみるthis.copyと、実質的に同じエラーが発生します。

[error] Lift/framework/web/webkit/src/main/scala/net/liftweb/http/js/JsCommands.scala:452: value copy is not a member of net.liftweb.http.js.JE.JsVar
[error]     def -&(right: String) = this.copy(andThen=(right :: andThen.toList.reverse).reverse :_*)
[error]

copyこれはなぜですか?また、ケース クラス メソッドでどのように使用できますか? copyそれとも、私のメソッドを宣言した後にコンパイラが追加するアイデアですか?

私はこれを行う必要がありますか?

case class JsVar(varName: String, andThen: String*) extends JsExp {
  // ...
  def -&(right: String) = JsVar(varName, (right :: andThen.toList.reverse).reverse :_*)
}
4

1 に答える 1

7

仕様はこの点について言及していませんが、これは実際に想定されていることです。メソッドはデフォルト パラメータに依存し、copyデフォルト パラメータは繰り返しパラメータ (varargs) には使用できません。

パラメータが繰り返されるパラメータ セクションでは、デフォルトの引数を定義することはできません。

(Scala リファレンス、セクション 4.6.2 - 繰り返しパラメーター)

scala> def f(xs: Int*) = xs
f: (xs: Int*)Int*

scala> def f(xs: Int* = List(1, 2, 3)) = xs
<console>:24: error: type mismatch;
 found   : List[Int]
 required: Int*
       def f(xs: Int* = List(1, 2, 3)) = xs
                            ^
<console>:24: error: a parameter section with a `*'-parameter is not allowed to have default arguments
       def f(xs: Int* = List(1, 2, 3)) = xs
           ^
于 2011-07-09T22:41:13.023 に答える