3

如何使 REPL 导入命令行中给出的包?

样本:

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 时手动粘贴一些代码。我也不想使用 SBT 来在 REPL 启动后运行一个命令。

4

2 回答 2

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
于 2013-08-29T11:52:19.293 回答
0

我编写了一个脚本来自动化命令行参数以归档事物(基于来自som-snytt 的A )。它在带有MSYS的 win 7 上进行了测试,但它也应该在 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
于 2013-08-29T14:15:37.407 回答