【问题标题】:Why this Scala code doesn't type checked为什么这个 Scala 代码没有类型检查
【发布时间】:2016-06-04 17:19:24
【问题描述】:

我想写一个函数来计算二叉树的大小,函数'size'工作正常,但是太慢了。然后我编写了一个名为“size2”的函数,但是,它没有经过类型检查。为什么?

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]

def size[A](tree: Tree[A]): Int = tree match {
  case Leaf(_) => 1
  case Branch(l, r) => size(l) + size(r) + 1
}

def size2[A](tree: Tree[A]): Int = {
  trait Timing
  case object First extends Timing
  case object TravelLeft extends Timing
  case object TravelRight extends Timing
  type Stack = List[(Tree[A], Timing)]

  def count(history: Stack, res: Int): Int = history match {
    case Nil => res
    case (Leaf(_), _)::tail => count(tail, res + 1)
    case (b@Branch(l, _), First)::tail =>
      val next = (l, First)::(b, TravelLeft)::tail
      count(next, res)
    case (b@Branch(_, r), TravelLeft)::tail =>
      val next = (r, First)::(b, TravelRight)::tail
      count(next, res)
    case (_, TravelRight)::tail => count(tail, res + 1)
  }
  count(List((tree, First)), 0)
}

错误信息如下:

Error:(19, 23) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(A$A301.this.Tree[A], Timing)]
    case (Leaf(_), _)::tail => count(tail, res + 1)
                     ^
Error:(21, 34) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(A$A301.this.Tree[A], Timing)]
    case (b@Branch(l, _), First)::tail =>
                                ^
Error:(24, 39) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(A$A301.this.Tree[A], Timing)]
    case (b@Branch(_, r), TravelLeft)::tail =>
                                     ^
Error:(27, 27) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(A$A301.this.Tree[A], Timing)]
    case (_, TravelRight)::tail => count(tail, res + 1)
                         ^
Error:(66, 23) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(inst$A$A.Tree[A], Timing)]
    case (Leaf(_), _)::tail => count(tail, res + 1)
                     ^
Error:(68, 34) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(inst$A$A.Tree[A], Timing)]
    case (b@Branch(l, _), First)::tail =>
                                ^
Error:(71, 39) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(inst$A$A.Tree[A], Timing)]
    case (b@Branch(_, r), TravelLeft)::tail =>
                                     ^
Error:(74, 27) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[B]
 required: test.List[(inst$A$A.Tree[A], Timing)]
    case (_, TravelRight)::tail => count(tail, res + 1)
                         ^

【问题讨论】:

  • 你的 scala 版本是什么?看起来像是为我编译的。
  • 我的scala版本是2.11.7
  • 我用的是同一个版本。我只收到以下警告:“它会在以下输入上失败:List(_) def count(history: Stack, res: Int): Int = history match { ^ one warning found”我复制粘贴了您的代码并将您的对象内的两个大小方法。
  • 我找到了原因,因为我在scala工作表中编码。但为什么它在 scala 工作表中不起作用?这是一个错误吗?
  • 您使用的是 ScalaIDE (Eclipse) 还是 IntelliJ Scala 插件?我认为这两种工具都有自己的编译器前端(解析器/类型检查器等),并且它们推断类型的方式可能存在错误。然而,如果 scalac 编译它,这意味着你的代码是正确的。

标签: scala types functional-programming type-systems


【解决方案1】:

========================更新======================= ====

感谢 som-snytt 的帮助。引发此错误是因为我在同一个包中定义了一个列表。看到错误信息,它想要一个test.List的对象,并且::对象创建一个标准库的List,它没有类型检查

================================================ =========

好的,感谢 Wickoo 的评论。我在 Scala 文件中再次尝试过它,它工作正常。这个错误只会出现在我不知道为什么的 scala 工作表中。

【讨论】:

  • 显而易见的问题是,您是否在某处定义了 test.List 类?如果您使用的是编译服务器,也许只需进行干净的重建。
猜你喜欢
  • 1970-01-01
  • 2011-02-04
  • 2020-11-01
  • 1970-01-01
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
相关资源
最近更新 更多