I have some code like this:
case class FunctionCommand[A](function: Function1[Array[A], Unit])
class MyClass(commands: List[FunctionCommand[_]]) {
def foo(parametersForEachFunction: Seq[Array[_]]) {
assert(commands.size == parametersForEachFunction.size)
for ((command, parameter) <- commands zip parametersForEachFunction) {
command.function(parameter)
}
}
}
And it does not compile:
MyClass.scala:7: type mismatch;
found : Array[(some other)_0(in value $anonfun)] where type (some other)_0(in value $anonfun)
required: Array[_0(in value $anonfun)] where type _0(in value $anonfun)
command.function(parameter)
^
I wonder what Array[(some other)_0(in value $anonfun)]
is. Can I write something like command.function(parameter.asInstanceOf[????])
to let it compiles?
I have a workaround. I replace command.function(parameter)
to:
def forceInvoke[A](command: FunctionCommand[A], parameter: Any) {
command.function(parameter.asInstanceOf[A])
}
forceInvoke(command, parameter)
And it compiles.
But I still want to know if there is a way to cast the parameter
to correct type at runtime inlinely.
It's enough to write it like this:
for ((command: FunctionCommand[_], parameter) <- commands zip parametersForEachFunction) {
command.function(parameter)
}