Scala 解释器的局限性和解决方法是什么? [英] What are the limitations and walkarounds of Scala interpreter?
问题描述
什么样的结构需要'scalac'编译以及如何制作一个可以在解释器中工作的等价物?
What kind of constructs need 'scalac' compile and how to make an equivalent that will work in interpreter?
我想使用 scala 而不是 python 作为脚本语言.(使用#!/usr/bin/scala)
I want to use scala instead of python as a scripting language. (with #!/usr/bin/scala)
推荐答案
你应该能够在 REPL 中做任何你可以在外部代码中做的事情.请记住:
You ought to be able to do anything in the REPL that you can do in outside code. Keep in mind that:
相互循环引用的事物需要在一个块内.因此不能按原样输入以下内容;你必须将它包装在其他对象中:
Things that refer to each other circularly need to be inside one block. So the following can't be entered as-is; you have to wrap it inside some other object:
class C(i : Int) { def succ = C(i+1) }
object C { def apply(i: Int) = new C(i) }
执行环境有些不同,因此基准测试的时间并不总是与从编译代码运行它们的方式相同.
The execution environment is somewhat different, so benchmarking timings will not always come out the same way as if you run them from compiled code.
您以不同的方式输入执行路径;如果你想调用一个 main
方法,你当然可以从 REPL 内部调用.
You enter the execution path a different way; if you want to call a main
method, though, you certainly can from inside the REPL.
您不能只是将整个库剪切并粘贴到 REPL 中,然后让它像库一样工作;REPL 具有与普通包不同的结构.因此,请在测试期间删除包"声明.
You can't just cut-and-paste an entire library into the REPL and have it work exactly like the library did; the REPL has a different structure than normal packages do. So drop the "package" declarations during testing.
这篇关于Scala 解释器的局限性和解决方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!