Scala:使用mutable.ArrayBuffer调用fun时类型不匹配

时间:2017-06-02 03:23:03

标签: scala

我对Scala感到愚蠢:(我在调用和Scala类型方面有一些帮助。但新的再次出现:

class TagCalculation {
  def test(arg1: java.lang.Integer, arg2: String, arg3: scala.collection.immutable.$colon$colon[Any]) = "test mix2"
}

val getTest =  new TagCalculation
val arg1: java.lang.Integer = 10
val arg2: String = "foobar"
val arg3: scala.collection.immutable.$colon$colon[Any] = scala.collection.immutable.$colon$colon.apply('z', List("foo", 10))

//val test = Array(arg1,arg2,arg3) It's Ok if i use that.
var calcParamsArray : scala.collection.mutable.ArrayBuffer[Any] = scala.collection.mutable.ArrayBuffer()

calcParamsArray += arg1
calcParamsArray += arg2
calcParamsArray += arg3

val argtypes4 = calcParamsArray.map(_.getClass)
val method4 = getTest.getClass.getMethod("test", argtypes4: _*)
method4.invoke(getTest,calcParamsArray: _*) //also errors in toArray and toSqe

输出:

  

阶> method4.invoke(getTest,calcParamsArray:_ ):29:   错误:类型不匹配;发现:   scala.collection.mutable.ArrayBuffer [Any] required:Seq [Object]                 method4.invoke(getTest,calcParamsArray:_

有什么想法解决这个问题?

1 个答案:

答案 0 :(得分:2)

错误消息明确说明

found : scala.collection.mutable.ArrayBuffer[Any] 
required: Seq[Object] 

您正在传递ArrayBuffer[Any],但必需的是Seq[Object]

将代码的最后一行更改为

method4.invoke(getTest,calcParamsArray.asInstanceOf[Seq[Object]]: _*)

应该有效