Java 代码
String method1(Object obj) {
if (obj == null) {
return "null";
} else if (obj instanceof MyClass123[]) {
return method2(ob)).toString();
} else if (obj instanceof int[]) {
return Arrays.toString((int[]) obj);
} else if // and so for double, float, boolean, long, short...
} else if (obj instanceof Object[]) {
return Arrays.deepToString((Object[]) obj);
} else {
return obj.toString();
}
}
我尝试在 Scala 中做同样的事情
def method1(obj: Any): String = obj match {
case null => "null"
case x: Array[MyClass123] => method2(x).toString
case x: Array[AnyRef] => Arrays.deepToString(x)
case x: Array[_] => Arrays.toString(x)
}
最后一行导致错误
overloaded method value toString with alternatives:
[error] (Array[java.lang.Object])java.lang.String <and>
[error] (Array[Double])java.lang.String <and>
[error] (Array[Float])java.lang.String <and>
[error] (Array[Boolean])java.lang.String <and>
[error] (Array[Byte])java.lang.String <and>
[error] (Array[Char])java.lang.String <and>
[error] (Array[Short])java.lang.String <and>
[error] (Array[Int])java.lang.String <and>
[error] (Array[Long])java.lang.String
[error] cannot be applied to (Array[_])
[error] case x: Array[_] => Arrays.toString(x)