次の Java コードを Scala コードに変換したい:
Object method1(Object ... objArray) {
if (objArray instanceof MyClass1[]) {
Object resArray[] = new Object[objArray.length];
for (int i = 0; i < objArray.length; i++) {
resArray[i] = objArray[i].toString();
}
return resArray;
} else {
List<Object> resArray = new ArrayList<Object>();
for (int i = 0; i < objArray.length; i++) {
for (Object obj: scalar(objArray[i])) {
resArray.add(obj);
}
}
return resArray.toArray();
}
}
//..........
private static class MyClass1 {
private Object obj;
public MyClass1(Object obj) {
this.obj = obj;
}
@Override
public String toString() {
return obj.toString();
}
}
これが私が持っているもので、エラーが発生します:
def method1(objs: Any*): Array[_] = objs match {
case x: Array[MyClass1] => x.map(toString) // MyClass1 looks like
case x => x.map(method2).toArray
}
//...................
def method2(obj: Any): Array[_] = {....} //it's perfectly fine
class MyClass1 (obj: AnyRef) {
override def toString = obj.toString
}
エラー:
1)pattern type is incompatible with expected type;
[error] found : Array[MyClass1]
[error] required: Any*
[error] case x: Array[MyClass1] => x.map(toString)
2)type mismatch;
[error] found : Array[MyClass1]
[error] required: Any*
[error] case x: Array[MyClass1] => x.map(toString)
3)could not find implicit value for evidence parameter of type ClassManifest[Array[_]]
[error] case x => x.map(method2).toArray
これを解決するにはどうすればよいですか?