Scala REPL使用指南及代码编译、反编译技巧
1. 向REPL粘贴和加载代码块
在Scala REPL中进行代码实验时,直接输入或粘贴代码可能会遇到问题。这是因为REPL具有“贪婪”特性,会在遇到完整语句时立即处理,导致粘贴代码块失败。以下是解决方法:
1.1 使用 :paste
命令
直接粘贴以下 if/else
代码块会出错:
if (true)
print("that was true")
else
print("that was false")
正确做法是先输入 :paste
命令,再粘贴代码,最后按 Ctrl - D
:
scala> :paste
// Entering paste mode (ctrl - D to finish)
if (true)
print("that was true")
else
print("false")
[Ctrl - D]
// Exiting paste mode, now interpreting.
that was true
具体步骤如下:
1. 在REPL中输入 :paste
命令。
2. 粘贴代码块(Mac使用 Command - V
,Windows使用