【问题标题】:Code compiles with scalac but not in REPL代码用 scalac 编译,但不在 REPL 中
【发布时间】:2012-04-27 01:12:16
【问题描述】:

我有一些代码,比如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
                  ^

【问题讨论】:

    标签: scala read-eval-print-loop


    【解决方案1】:

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

    尝试使用“粘贴模式”将多个块粘贴到 REPL 中,以便 Scala 一起编译:

    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
    

    【讨论】:

    • 感谢提示:粘贴模式!
    猜你喜欢
    • 1970-01-01
    • 2012-11-10
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多