54
使用可变参数我撕裂了我的头发试图找出如何做到以下几点:从斯卡拉
def foo(msf: String, o: Any, os: Any*) = {
println(String.format(msf, o :: List(os:_*)))
}
还有一个原因,我不得不宣布与o
和os
Seq
单独的方法。基本上,我最终使用单个对象参数(类型List
)调用的格式方法。尝试:
def foo(msf: String, o: Any, os: Any*) = {
println(String.format(msf, (o :: List(os:_*))).toArray)
}
给我的错误类型:
found: Array[Any]
required Seq[java.lang.Object]
我已经试过铸造,其中编译,但失败了几乎相同的理由,第一个例子。当我尝试
println(String.format(msg, (o :: List(os:_*)) :_*))
失败与隐式转换歧义(any2ArrowAssoc
和any2stringadd
)
它编译但在运行时翻倒: 异常在线程“主要” java.lang.ClassCastException:斯卡拉$结肠结肠$不能转换为scala.runtime.BoxedObjectArray \t在scala.runtime.RichString .format(RichString.scala:242) – 2009-06-18 06:30:16
另外,toList从哪里来? – 2009-06-18 07:43:03