Scala 作为脚本语言
可能的重复:
Scala 中的“eval”
我知道 scala 是一种编译语言,但我也知道我可以动态地将类加载到 jvm 中,我可以在运行时调用 scala 编译器,最后但并非最不重要的一点是我也有一个很棒的 repl,因此将 scala 作为脚本语言应该是可能的。
所以我需要运行一些任务:
简单解释:
val src = """ println("Hello World") """
interpret(src)
调用外部函数:
object A{
def foo =
println("Hello World")
}
val src = """ A.foo """
interpret(src)
实现功能:
trait T{
def foo:String
}
val src = """ class A extends T{ def foo = "Hello World" } """
interpret(src)
val t = loadClassAndCreatInstance.asInstanceOf[T]
println(t.foo)
如果能找到解决我所有问题的方法,那就太棒了。
Possible Duplicate:
“eval” in Scala
I know scala is a compiled language, but I do also know that I can dynamically load classes into the jvm, and I can call the scala compiler at runtime, last but not least I do also have an awesome repl, so having scala as a scripting language should be possible.
so there are some tasks I need to get to run:
simple interpret:
val src = """ println("Hello World") """
interpret(src)
call external functions:
object A{
def foo =
println("Hello World")
}
val src = """ A.foo """
interpret(src)
implement functionality:
trait T{
def foo:String
}
val src = """ class A extends T{ def foo = "Hello World" } """
interpret(src)
val t = loadClassAndCreatInstance.asInstanceOf[T]
println(t.foo)
it would be great to get a solution to all my problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知怎的,我已经找到了如何使用 scala 作为脚本语言,但我仍然对 classLoader 有问题
somehow I already found out how to use scala as scripting language but I still have a problem with the classLoader
请参阅我的答案:Scala 中的“eval”
(我假设您已经知道scala 交互模式,以及如何使用 Scala 兼容的 shebang
#!
行编写可执行脚本。)See my answer here: "eval" in Scala
(I assume you are already aware of scala interactive mode, and how to write executable scripts with a Scala-compatible shebang
#!
line.)