代码scalac编译但不是在REPL(Code compiles with scalac but n

2019-10-16 16:07发布

我有一些代码,说在Foo.scala与容易编译scalac ,但我得到错误的暴风雪,当我启动了REPL说:load Foo.scala 。 我想这是标准和记载,但似乎无法找到关于它的任何相关信息。

该文件是这样的:

abstract class BST[A](implicit cmp: A => Ordered[A]) {
  def fold[B](f: (B, A) => B, acc: B): B = {
    this match {
      case Leaf()        => acc
    }                 
  } 
} 

case class Leaf[A]()(implicit cmp: A => Ordered[A]) extends BST[A]

而我得到像这样的错误:

scala> :load BST3.scala
Loading BST3.scala...
<console>:10: error: constructor cannot be instantiated to expected type;
 found   : Leaf[A(in class Leaf)]
 required: BST[A(in class BST)]
             case Leaf()        => acc
                  ^

Answer 1:

它看起来像:load试图解释文件块逐块。 由于您的块是互相依赖的,这是一个问题。

尝试使用“粘贴模式”,以多块粘贴到REPL斯卡拉编译在一起:

scala> :paste

// Entering paste mode (ctrl-D to finish)

abstract class BST[A](implicit cmp: A => Ordered[A]) {
  def fold[B](f: (B, A) => B, acc: B): B = {
    this match {
      case Leaf()        => acc
    }                 
  } 
} 

case class Leaf[A]()(implicit cmp: A => Ordered[A]) extends BST[A]

// Exiting paste mode, now interpreting.

defined class BST
defined class Leaf


文章来源: Code compiles with scalac but not in REPL