我有一个类型化的pair类:
class TypedPair[T]
我想将某个函数应用于它们的异构序列:
def process[T](entry: TypedPair[T]) = {/* something */}
为什么这不起作用?
def apply(entries: TypedPair[_]*) = entries.foreach(process)
失败并出现错误:
error: polymorphic expression cannot be instantiated to expected type;
found : [T](TypedPair[T]) => Unit
required: (TypedPair[_]) => ?
def apply(entries: TypedPair[_]*) = entries.foreach(process)
我不记得在 Java 中遇到过这个问题...
最佳答案
在这种情况下,编译器在识别匿名方法时遇到问题。当您添加虚拟参数时,您还更改了语法以帮助编译器使用它,因此以下内容将起作用:
def apply(entries: TypedPair[_]*) = entries.foreach(process(_))
关于scala - 为什么Scala不能绑定(bind)通配符类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4302656/