如何从命令行向REPL添加导入?

时间:2013-08-29 10:59:28

标签: scala read-eval-print-loop

如何让REPL导入commnad行中给出的包?

样品:

scala -someMagicHere "import sys.error"
scala> :imports
1) import scala.Predef._          (162 terms, 78 are implicit)
2) import sys.error               (2 terms)

scala> _

PS:重复。我想要自动化解决方案,而不是每次运行REPL时手动粘贴一些代码。另外,我不想仅在启动后在REPL中运行一个命令时使用SBT。

2 个答案:

答案 0 :(得分:5)

将其粘贴在文件中。

apm@mara:~/tmp$ scala -i imports.script
Loading imports.script...
import sys.error

Welcome to Scala version 2.10.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :impo
 1) import scala.Predef._          (162 terms, 78 are implicit)
 2) import sys.error               (2 terms)

编辑:

我认为您可以获得寻找或引发或诱导错误的奖励积分:

apm@mara:~/tmp$ scala -e "import sys.error"
java.lang.ClassNotFoundException: Main

答案 1 :(得分:0)

我写了一个脚本来自动化命令行参数到文件的东西(基于来自som-snytt的A)。它在win 7上使用MSYS进行了测试,但它也适用于Linux。

bash-3.1$ repl "import sys.error"
Loading C:\Users\xxx\AppData\Local\Temp\tmp.GgEjauEqwz...
import sys.error

Welcome to Scala version 2.10.0 (Java HotSpot(TM) Client VM, Java 1.7.0_07).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :impo
 1) import scala.Predef._ (162 terms, 78 are implicit)
 2) import sys.error (2 terms)
#!/bin/bash

function crash {
    echo "Error: $1"
    exit 10
}

if [ $# -ne 1 ]; then
    echo "Script requires one parameter."
    exit 1
fi

t=$(mktemp) || crash "Unable to create a temp file."
[ ${#t} -lt 1 ] && crash "Suspiciously short file name, aborting."
trap "rm -f -- '$t'" EXIT

echo "$1" > "$t"
scala -i "$t" || crash "Something wrong with scala binary."

rm -f -- "$t"
trap - EXIT
exit
相关问题