简体   繁体   English

在Scala REPL中`:load`有什么用,什么没有用?

[英]What does `:load` do and what does it not do in Scala REPL?

I have the following code: 我有以下代码:

abstract class AExp {
  def eval : Int = this match {
    case Num(n) => n
    case Add(e1, e2) => e1.eval + e2.eval
  }
}

case class Num(n : Int) extends AExp
case class Add(e1 : AExp, e2 : AExp) extends AExp

case class Prd(e1 : AExp, e2 : AExp) extends AExp {
  override def eval : Int = e1.eval * e2.eval
}

It works fine under Scala 2.8 here but does not work under 2.10 when I load it into the REPL. 它工作正常的Scala 2.8下在这里 ,但2.10下不工作时,我将其加载到REPL。 I get tons of errors: 我有很多错误:

scala> :l aexp.scala
Loading aexp.scala...
<console>:9: error: not found: value Num
           case Num(n) => n
                ^
<console>:9: error: not found: value n
           case Num(n) => n
                          ^
<console>:10: error: not found: value Add
           case Add(e1, e2) => e1.eval + e2.eval
                ^
<console>:10: error: not found: value e1
           case Add(e1, e2) => e1.eval + e2.eval
                               ^
<console>:7: error: not found: type AExp
       case class Num(n : Int) extends AExp
                                       ^
<console>:7: error: not found: type AExp
       case class Add(e1 : AExp, e2 : AExp) extends AExp
                                                    ^
<console>:7: error: not found: type AExp
       case class Add(e1 : AExp, e2 : AExp) extends AExp
                           ^
<console>:7: error: not found: type AExp
       case class Add(e1 : AExp, e2 : AExp) extends AExp
                                      ^
<console>:7: error: not found: type AExp
       case class Prd(e1 : AExp, e2 : AExp) extends AExp {
                                                    ^
<console>:7: error: not found: type AExp
       case class Prd(e1 : AExp, e2 : AExp) extends AExp {
                           ^
<console>:7: error: not found: type AExp
       case class Prd(e1 : AExp, e2 : AExp) extends AExp {
                                      ^

But the same code works fine in :paste mode under the 2.10 REPL. 但是相同的代码在2.10 REPL下的:paste模式下也可以正常工作。 What is going on here? 这里发生了什么? What does :load do and what does it not do? :load做什么,不做什么?

As :help says, :help所说,

:load :加载
load and interpret a Scala file 加载并解释一个Scala文件

it loads file and interprets it line-by-line , just like it would interpret individual lines that you feed to REPL, whereas :paste interpret whole chunk of code as an atomical unit. 它加载文件并逐行 解释它,就像它将解释您提供给REPL的每一行一样,而:paste将整个代码块视为一个原子单元。

The error, as you can predict is that repl sees 您可以预测的错误是repl看到了

abstract class AExp {
  def eval : Int = this match {
    case Num(n) => n
    case Add(e1, e2) => e1.eval + e2.eval
  }
}

But know nothing about Num and Add types -- they aren't defined yet. 但是对Num和Add类型一无所知-它们尚未定义。 The trick to make :load work the way you wanted to is to wrap all your code in some super object, eg: 使:load以您想要的方式工作的技巧是将所有代码包装在某个超级对象中,例如:

object InterpretAsUnit {
  // all your code goes there
}

PS funny fact, is that scala -i <your path> nearly identical to scala and then :load <your path> PS有趣的事实是, scala -i <your path>scala几乎相同,然后:load <your path>

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

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