I have scala code as following
object Main extends App {
def getFun[T, U](x: Array[T] => U, p: Array[T]): U = { x(p) }
def getFun2(x: Array[Int] => Double, p: Array[Int]): Double = { x(p) }
val arr = Array(1, 2, 3, 4)
println(getFun((x: Array[Int]) => x.sum, arr))
// println(getFun(_.sum, arr)) // error, why!
// println(getFun(x = _.sum, arr)) // error, why!
println(getFun2(_.sum.toDouble, p = arr))
}
Why getFun cannot be used as simple as getFun2. I think use the "x = _.sum", the scala compiler can find the correct type, but it seems that it failed.
Can any body explain the reason of the error.
Thanks!