代码使用 scalac 编译但不在 REPL 中 [英] Code compiles with scalac but not in REPL

查看:45
本文介绍了代码使用 scalac 编译但不在 REPL 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,在 Foo.scala 中说,它很容易用 scalac 编译,但是当我启动 REPL 并说 :load Foo.scala 时,我遇到了大量错误.我想这是标准的并且有记录,但似乎找不到任何相关的相关信息.

I have some code, say in Foo.scala that compiles readily with scalac, but I get a blizzard of errors when I boot up the REPL and say :load Foo.scala. I imagine this is standard and documented but can't seem to find any relevant information about it.

文件如下所示:

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
                  ^

推荐答案

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

It looks like :load tries to interpret the file block-by-block. Since your blocks are mutually-dependent, this is a problem.

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

Try using "paste mode" to paste multiple blocks into the REPL for Scala to compile together:

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

这篇关于代码使用 scalac 编译但不在 REPL 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆