Scala REPL 怎么用?Scala 交互式解释器入门使用方法?

文章导读
Previous Quiz Next Scala REPL 简介 REPL 是 Read Evaluate Print Loop 的缩写,它是一个命令行工具。REPL 用于表达式、评估和执行小型代码片段。就像 Java Shell 一样,Scala REPL 非常
📋 目录
  1. Scala REPL 简介
  2. REPL 的实现
  3. REPL 安装
  4. REPL 的有用命令和特性
  5. 处理 REPL 的依赖
  6. REPL 的主要特性
A A

Scala - REPL



Previous
Quiz
Next

Scala REPL 简介

REPLRead Evaluate Print Loop 的缩写,它是一个命令行工具。REPL 用于表达式、评估和执行小型代码片段。就像 Java Shell 一样,Scala REPL 非常适合初学者以及想要尝试新库或语言特性的人。

首先,REPL 读取在 Scala 命令行上提供的输入。然后评估该输入并在屏幕上显示结果。它准备好读取下一个输入,处理过程在循环中继续。在当前输入的作用域内,先前结果会根据需要自动导入。REPL 在交互模式下提示符处读取输入表达式。然后将这些表达式转换为可执行模板,编译并执行结果。

REPL 的实现

  • 您可以使用 switch -Yrepl-class-based 来将用户代码中的 object 和 class 包装起来。
  • 每一行输入都会单独编译。
  • 自动生成的 imports 包括先前行的依赖。
  • 您可以通过提供显式 import 来控制 scala.Predef 的隐式导入。

REPL 安装

安装 Scala 时,Scala REPL 就已经包含在内。因此,只要您的计算机上安装了 Scala,就可以随时使用它。您需要在终端中运行 "scala" 命令,然后终端中会出现以下内容:

Welcome to Scala 3.3.1 (21.0.1, Java Java HotSpot(TM) 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala>

REPL 的有用命令和特性

在终端 shell 中运行 Scala 非常有用。REPL 包含许多有用的命令和特性。我们已经讨论了一些。

1. help 命令

你需要输入 :help 命令,它会打印可用命令列表:

scala> :help
The REPL has several commands available:

:help                    print this summary
:load <path>             interpret lines in a file
:quit                    exit the interpreter
:type <expression>       evaluate the type of the given expression
:doc <expression>        print the documentation for the given expression
:imports                 show import history
:reset [options]         reset the repl to its initial state, forgetting all session entries
:settings <options>      update compiler options, if possible

scala>

2. load 命令

:load 命令从文件中加载并运行代码到 REPL 中。

例如,如果你有一个名为 script.scala 的文件,内容如下:

case class Person(name: String, Roll:Int)
val me = Person("Mithlesh", 101)

你可以使用 :load 命令运行这个文件:

scala> :load script.scala
// defined case class Person
val me: Person = Person(Mithlesh,101)

scala> me
val res0: Person = Person(Mithlesh,101)

scala>

3. paste 命令

使用 REPL 时遇到的第一个障碍之一是,默认情况下代码是逐行求值的。因此,像函数这样的多行代码无法执行,除非使用 paste 命令。此命令用于在 REPL 中粘贴和加载代码。首先在 REPL 中输入 :paste,然后粘贴你的代码块。

4. lastException 绑定

lastException 绑定到最后抛出的异常。例如,

scala> throw new NullPointerException("Oops, a null reference!")
java.lang.NullPointerException: Oops, a null reference!
  ... 32 elided

scala> lastException
res4: Throwable = java.lang.NullPointerException: Oops, a null reference!

5. reset 命令

如果你想从 REPL 会话中清除变量或函数,可以使用 :reset 命令。重置会话后,你将无法再使用之前的定义。

例如,

scala> val x = 42
x: Int = 42

scala> def bar(): String = "hello"
bar: ()String

scala> :reset
Resetting interpreter state.
Forgetting this session history:

val x = 42
def bar(): String = "hello"

Forgetting all expression results and named terms: x, bar

scala> x
<console>:12: error: not found: value x
       x
       ^

6. silent 命令

系统在你创建变量或函数时会显示定义。你可以使用 silent 命令禁用它。

例如,

scala> val x = 42
x: Int = 42

scala> :silent

scala> val y = "Hello, Scala!"

scala>

7. quit 命令

要结束 REPL 会话,你可以使用 :quit 命令或按 ctrl-D。

处理 REPL 的依赖

虽然 Scala REPL 不是为大型项目设计的,但通过向 classpath 添加额外的 jar 文件,它可以非常有用。

1. require 命令

此命令将 jar 添加到 classpath。例如,

scala> :require my-library-1.0.jar
Added '/path/to/my-library-1.0.jar' to classpath.

scala>

2. scala -cp

你可以在启动 REPL 时使用 -cp 参数包含 jar。例如,

scala -cp my-library-1.0.jar myScript.scala

3. sbt Console

在 sbt 项目中使用 sbt console 是将更多依赖引入 REPL 会话的另一种方法。当你使用 sbt console 命令时,它会启动一个 REPL,并将所有 sbt 依赖包含在 classpath 中。

REPL 的主要特性

  • IMain Binding − REPL 的 IMain 与 $intp 绑定。
  • Tab Key for Completion − 使用 tab 键进行自动补全。
  • lastException Binding − lastException 与 REPL 中的最后一个异常绑定。
  • :load Command − 使用 :load 加载包含 REPL 输入的文件。
  • :javap Command − 使用 :javap 检查 class 工件。
  • -Yrepl-outdir Switch − 使用 -Yrepl-outdir 以使用外部工具检查 class 工件。
  • :power Command − 进入 compiler 模式后导入 compiler 组件。
  • :help Command − 获取用户帮助的命令列表。