将 Scala 文件加载到解释器中以使用函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7383436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:26:58  来源:igfitidea点击:

Load Scala file into interpreter to use functions?

scalainterpreter

提问by Jamil

I have some Scala functions defined in a file, not in a class, and I would like to use them in the Scala interpreter. I know I can say scala filename.scalato simply run the file and exit the interpreter, but I would like to run the file and then stay in the interpreter so I can do some testing. Can anyone tell me how to simply load a file into the interpreter so I can use the functions defined within it?

我在文件中定义了一些 Scala 函数,而不是在类中,我想在 Scala 解释器中使用它们。我知道我可以说scala filename.scala简单地运行文件并退出解释器,但我想运行文件然后留在解释器中以便我可以做一些测试。谁能告诉我如何简单地将文件加载到解释器中,以便我可以使用其中定义的函数?

回答by Jamil

type :load /path/to/filein Scala REPL.

输入:load /path/to/fileScala REPL。

You can get complete list of available commands by typing :help

您可以通过键入来获取可用命令的完整列表 :help

回答by Suresh Babu

On occasions, :pastemight be your better friend (than :load). Here is an example on how to use :paste.

有时,:paste可能是你更好的朋友(比:load)。这是一个关于如何使用:paste的示例。

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

One can also use :pasteto load a file using following command :paste [path]

还可以:paste使用以下命令加载文件:paste [path]

scala> :paste ~/Desktop/repl_seeder.scala
Pasting file ~/Desktop/repl_seeder.scala...
defined object test1

scala> test1.main(Str)
my first scala program

回答by Esmaeil MIRZAEE

Just reminder, put the complete path. I found problem in Linux by doing like this:

只是提醒,把完整的路径。我通过这样做在 Linux 中发现了问题:

:load ~/fileName.scala

:load ~/fileName.scala

to get rid of error "That file does not exist" I did

摆脱错误“那个文件不存在”我做了

:load /complete/path/fileName.scala

:load /complete/path/fileName.scala