让我们考虑以下功能:
def printPfType[T](pf:PartialFunction[T, _])(implicit m:Manifest[T]) = {
println(m.toString)
}
Run Code Online (Sandbox Code Playgroud)
然后我定义以下测试类:
case class Test(s:String, i:Int)
Run Code Online (Sandbox Code Playgroud)
我写不出来:
printPfType {
case Test(_,i) => i
}
Run Code Online (Sandbox Code Playgroud)
因为编译器无法推断出PartialFunction的第一个参数类型.我必须明确指定它:
printPfType[Test] {
case Test(_,i) => i
}
Run Code Online (Sandbox Code Playgroud)
但那时Test类型会出现两次.有没有一种技术可以避免这种情况?如何帮助类型提供者避免重复?