は、私はこのようないくつかのコードがあります:
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)
}
}
}
をそして、それはコンパイルされません:
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)
^
私はArray[(some other)_0(in value $anonfun)]
があるのだろうか。コンパイルできるようにcommand.function(parameter.asInstanceOf[????])
のようなものを書くことはできますか?
回避策があります。私はにcommand.function(parameter)
を置き換える:
def forceInvoke[A](command: FunctionCommand[A], parameter: Any) {
command.function(parameter.asInstanceOf[A])
}
forceInvoke(command, parameter)
そして、それはコンパイルされます。
しかし、parameter
を実行時に正式に正しい型にキャストする方法があるかどうかはまだ分かります。
あなたを使用してください回避策。あなたの目標は、Scalaタイプのシステムがあなたが望む柔軟性を得る方法です。 – huynhjl
あなたはタイプを失って元に戻したくありません。型が必要な場合は、どこでも '_'を使用しないでください。異なる既知の型のリストが必要な場合は、 'HList'が必要です。その場合は[shapeless](https://github.com/milessabin/shapeless)を参照してください。 –
私はエンジニアであり、数学者ではありません。キーボードには「λ」、「ℤ」、「⊤」などはありません。 –