ScalaREPLの紹介
1. 序章
Scala REPLは、小さなコードスニペットの式、評価、および実行のためのコマンドラインツールです。 頭字語REPLは「Read-Evaluate-Print-Loop」の略です。
Java Shellと同様に、Scala REPLは、初心者や、新しいライブラリや言語機能を試してみたい人にとって非常に便利です。
2. インストールと使用法
Scala REPLにはScalaバイナリが付属しているため、Scalaがインストールされている限り使用できます。 Scala REPLセッションを開始するには、ターミナルで「scala」コマンドを実行する必要があります。 これは、REPLセッションを開始するとターミナルに表示されるものです。
Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_151).
Type in expressions for evaluation. Or try :help.
scala>
3. 便利なコマンドと機能
ターミナルシェルでScalaを実行すると便利です。また、REPLには豊富なコマンドと機能のセットが付属しています。 このセクションでは、最も有用なもののいくつかを示します。
3.1. helpコマンド
予想どおり、:help コマンドは、使用可能なコマンドのリストを出力します。
scala> :help
All commands can be abbreviated, e.g., :he instead of :help.
:edit <id>|<line> edit history
:help [command] print this summary or command-specific help
:history [num] show the history (optional num is commands to show)
:h? <string> search the history
:imports [name name ...] show import history, identifying sources of names
:implicits [-v] show the implicits in scope
:javap <path|class> disassemble a file or class name
:line <id>|<line> place line(s) at the end of history
:load <path> interpret lines in a file
:paste [-raw] [path] enter paste mode or paste a file
:power enable power user mode
:quit exit the interpreter
:replay [options] reset the repl and replay all previous commands
:require <path> add a jar to the classpath
:reset [options] reset the repl to its initial state, forgetting all session entries
:save <path> save replayable session to a file
:sh <command line> run a shell command (result is implicitly => List[String])
:settings <options> update compiler options, if possible; see reset
:silent disable/enable automatic printing of results
:type [-v] <expr> display the type of an expression without evaluating it
:kind [-v] <type> display the kind of a type. see also :help kind
:warnings show the suppressed warnings from the most recent line which had any
3.2. loadコマンド
:load コマンドは、REPLコードのファイルをロードして実行します。
現在のパスに次のファイルがあると仮定します。
// replScript.scala
case class Person(name: String, age: Int)
val me = Person("Manolis", 34)
ファイルを実行するには、:loadコマンドを使用する必要があります。
scala> :load -v replScript.scala
Loading replScript.scala...
scala> case class Person(name: String, age: Int)
defined class Person
scala> val me = Person("Manolis", 34)
me: Person = Person(Manolis,34)
scala>
3.3. 貼り付けコマンド
REPLを使用しているときに最初に遭遇する障害の1つは、デフォルトでは、コードが行ごとに評価されるという事実です。
その結果、関数などの複数行のコードブロックは、:pasteコマンドなしでは評価できません。
3.4. lastExceptionバインディング
最後にスローされた例外は、lastExceptionにバインドされます。 いくつかの例外をスローして、lastExceptionがどのように機能するかを見てみましょう。
scala> throw new RuntimeException("REPL is fantastic!")
java.lang.RuntimeException: REPL is fantastic!
... 32 elided
scala> lastException
res4: Throwable = java.lang.RuntimeException: REPL is fantastic!
scala> throw new RuntimeException("REPL is awesome!")
java.lang.RuntimeException: REPL is awesome!
... 32 elided
scala> lastException
res6: Throwable = java.lang.RuntimeException: REPL is awesome!
scala>
3.5. resetコマンド
REPLセッションから変数または関数をクリアする必要がある場合は、:resetコマンドを使用できます。
セッションをリセットすると、以前の定義は使用できなくなります。
scala> val a = 1
a: Int = 1
scala> def foo(): String = "blah.."
foo: ()String
scala> :reset
Resetting interpreter state.
Forgetting this session history:
val a = 1
def foo(): String = "blah.."
Forgetting all expression results and named terms: a, foo
scala> a
<console>:12: error: not found: value a
a
^
scala>
3.6. silentコマンド
変数または関数を定義した後、定義は後で印刷されます。 :silent コマンドを使用して、定義印刷を無効にすることができます。
scala> val a = 1
a: Int = 1
scala> :silent
scala> val b = 2
scala>
3.7. quitコマンド
最後に、:quit コマンドを使用するか、ctrl-Dを押すことにより、REPLセッションを閉じることができます。
4. 依存関係の操作
Scala REPLは大きなタスクやプロジェクトを対象としていませんが、クラスパスにjarを追加して、作業を簡単にすることができます。
4.1. requireコマンド
:require コマンドは、クラスパスにjarを追加します。
scala> :require commons-lang3-3.12.0.jar
Added '/Users/mvarvarigos/scala-repl/commons-lang3-3.12.0.jar' to classpath.
scala>
4.2. scala -cp
-cp 引数を使用してREPLを起動すると、jarを追加できます。
scala -cp commons-lang3-3.12.0.jar
4.3. sbtコンソール
追加の依存関係をREPLセッションにインポートする別の方法は、sbtプロジェクト内でsbtコンソールを使用することです。 sbt コンソールコマンドは、クラスパスに追加されたすべてのsbt依存関係でREPLを起動します。
5. 結論
この記事では、最もよく使用されるScalaREPLの機能とコマンドのいくつかを説明しました。