繁体   English   中英

Scala本地类型推断下划线表示法

[英]Scala local type inference underscore notation

通过研究“ 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编译就好了,但深度2不意味着在最后一个函数的类型。

编写方法如下:

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

原来,在第一个方法中删除括号,删除所有编译错误,如下所示:

def depth2[A](t: Tree[A]): Int = fold(t)((_) => 0)((a,b) => 1 + a max b)

因此,下划线符号似乎总是选择最接近的范围来推断类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM