13

If I have two separate uncompiled scala files in the same directory as:

// hello.scala
object hello {
  def world() = println("hello world")
}

and:

// do.scala
hello.world()

I get an error when running do.scala:

$ scala do.scala
error: not found: value hello

Instead I have to compile the hello.scala file first and put it on the classpath to get it to work:

$ scalac hello.scala
$ scala -cp hello do.scala
hello world

Is there a way to get one script to call the other uncompiled scala file using the right use of import, package, classpath, the scala command line tool or something else?

4

3 回答 3

8

也许不完全是您正在寻找的东西,但您可以从 Scala REPL shell 中完成

:load hello.scala
:load do.scala

达到相同的结果:

$ scala
Welcome to Scala version 2.9.1 (Java HotSpot(TM) Server VM, Java 1.6.0_26).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :load hello.scala
Loading hello.scala...
defined module hello

scala> :load do.scala
Loading do.scala...
hello world

scala> 

如果您想要一些非交互式的脚本

$ cat <<EOF | scala
:load hello.scala
:load do.scala
EOF

也可以。

用于:helpREPL shell 可以做的更有趣的事情。

于 2012-04-04T12:59:05.353 回答
2

查看动态编译/嵌入编译器。Twitter 的util-eval就是这样一个例子。

于 2012-04-04T12:16:37.050 回答
0

你可以把

:load /path/file

在您要加载的文件的第一行。

于 2020-02-06T19:45:19.710 回答