通过研究“Scala中的函数式编程”,我想知道为什么以下代码会给出缺少的参数类型错误。
定义了以下树数据结构:
sealed trait Tree[+A]
case class Leaf[A](value: A) extends Tree[A]
case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]
以及以下方法:
object Tree {
def fold[A,B](t: Tree[A])(z: A => B)(f: (B,B) => B): B = t match {
case Leaf(v) => z(v)
case Branch(l,r) => f(fold(l)(z)(f), fold(r)(z)(f)) }
def size2[A](t: Tree[A]): Int = fold(t)((_) => 1)(_ + _ + 1)
def maximum2(t: Tree[Int]): Int = fold(t)((a) => a)(_ max _)
def depth2[A](t: Tree[A]): Int = fold(t)((_) => 0)(1 + (_ max _))
}
方法size2和maximum2可以很好地编译,但是depth2不能推断最后一个函数的类型。
编写方法如下:
def depth2[A](t: Tree[A]): Int = fold(t)((_) => 0)((a,b) => 1 + (a max b))
使其可以正常编译。
问:为什么Scala不能在带下划线符号的第一种方法上推断类型,而在第二种方法上能推断类型?是什么使其他方法可以正常编译?
谢谢你的帮助。
标尺版本:2.11.4
最佳答案
1 + (_ max _)
扩展为1 + ((a, b) => a max b)
,它向1添加了一个函数。如果指定了类型,则会出现另一个错误:
<console>:22: error: overloaded method value + with alternatives:
(x: Double)Double <and>
(x: Float)Float <and>
(x: Long)Long <and>
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int <and>
(x: String)String
cannot be applied to ((Int, Int) => Int)
def depth2[A](t: Tree[A]): Int = fold(t)((_) => 0)(1 + ((_: Int) max (_: Int)))
如您所见,您需要明确地放置参数
(a,b) => 1 + (a max b)
或跳过原谅
1 + _ max _
您实际上无法在此处执行此操作,因为它就像您说的那样工作
(a,b) => (1 + a) max b
关于scala - Scala本地类型推断下划线表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34705424/