Scala 作为脚本语言

发布于 2024-11-10 03:31:23 字数 777 浏览 0 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲欢浪云 2024-11-17 03:31:23

不知怎的,我已经找到了如何使用 scala 作为脚本语言,但我仍然对 classLoader 有问题

object O{
  def foo = println("Hello World in object O")
}

trait T{
  def foo:String
}

object MyInterpreter extends App{
  val srcA = 
  """ 
  println("Hello World from srcA") 
  """

  val srcB = """ O.foo """

  val srcC = """ 
  class A extends T{ 
    def foo = "Hello World from srcC"
    override def toString = "this is A in a src"
  }
  """


  val out = System.out
  val flusher = new java.io.PrintWriter(out)

  val interpreter = {
  val settings = new import scala.tools.nsc.GenericRunnerSettings( println _ )
  new scala.tools.nsc.interpreter.IMain(settings, flusher)
  }

  interpreter.interpret(srcA)
  interpreter.interpret(srcB)
  interpreter.compileString(srcC)

  val classA = interpreter.classLoader.findClass("A")

  println(classA)

  val constructors = classA.getDeclaredConstructors
  val myinstance = constructors(0).newInstance()

  println(myinstance)

  //this still throws an classCastException
  myinstance.asInstanceOf[T].foo 
  //but everything else works
}

somehow I already found out how to use scala as scripting language but I still have a problem with the classLoader

object O{
  def foo = println("Hello World in object O")
}

trait T{
  def foo:String
}

object MyInterpreter extends App{
  val srcA = 
  """ 
  println("Hello World from srcA") 
  """

  val srcB = """ O.foo """

  val srcC = """ 
  class A extends T{ 
    def foo = "Hello World from srcC"
    override def toString = "this is A in a src"
  }
  """


  val out = System.out
  val flusher = new java.io.PrintWriter(out)

  val interpreter = {
  val settings = new import scala.tools.nsc.GenericRunnerSettings( println _ )
  new scala.tools.nsc.interpreter.IMain(settings, flusher)
  }

  interpreter.interpret(srcA)
  interpreter.interpret(srcB)
  interpreter.compileString(srcC)

  val classA = interpreter.classLoader.findClass("A")

  println(classA)

  val constructors = classA.getDeclaredConstructors
  val myinstance = constructors(0).newInstance()

  println(myinstance)

  //this still throws an classCastException
  myinstance.asInstanceOf[T].foo 
  //but everything else works
}
誰認得朕 2024-11-17 03:31:23

请参阅我的答案: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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文