考虑一下:
scala> def sum(x:Int,y:Int) = x+y
sum: (x: Int, y: Int)Int
scala> sum(1,_:String)
<console>:9: error: type mismatch;
found : String
required: Int
sum(1,_:String)
显然 Scala 非常清楚 in 的确切类型,_
但sum(1,_)
您必须这样做 say sum(1,_:Int)
。为什么 ?
显然 Scala 随机(?)选择了一个:
scala> def sum(x:Int,y:String) = 1
sum: (x: Int, y: String)Int
scala> def sum(x:Int,y:Double) = 1
sum: (x: Int, y: Double)Int
scala> class Ashkan
defined class Ashkan
scala> sum(1,_:Ashkan)
<console>:10: error: type mismatch;
found : Ashkan
required: Double
sum(1,_:Ashkan)