Scala脚本在2.11 [英] Scala script in 2.11

查看:258
本文介绍了Scala脚本在2.11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到一个Scala运行时脚本的示例代码,回答从字符串生成一个类并在Scala 2.10中实例化它,但是代码似乎已过时2.11 - 我找不到对应于 build的任何函数。 setTypeSignature



如何在Scala 2.11中编译和执行Scala脚本?

$ b $让我们假设我想要如下:




  • 定义几个变量(名称和值)

  • 编译脚本

  • (可选改进)更改变量值

  • 执行脚本



为了简单起见,考虑以下示例:



我想定义以下变量(以编程方式,从代码,文本):

  val a = 1 
val s =String

我想要编译一个以下脚本并执行 String value a is 1,s is String从它返回:

 是$ a,s是$ s

我的函数应该是什么样子?

  def setupVariables()=? 

def compile()= ???

def changeVariables()= ???

def execute():String = ???


解决方案

Scala 2.11添加一个JSR-223 脚本引擎。它应该给你你正在寻找的功能。正如提醒一样,与所有这些种类的动态东西,包括在上面的描述中列出的示例,你将失去类型安全。您可以在下面看到返回类型始终为Object。



Scala REPL示例:

 code> scala> import javax.script.ScriptEngineManager 

import javax.script.ScriptEngineManager


scala> val e = new ScriptEngineManager()。getEngineByName(scala)

e:javax.script.ScriptEngine = scala.tools.nsc.interpreter.IMain@566776ad


scala> e.put(a,1)

a:Object = 1


scala> e.put(s,String)

s:Object = String


scala> e.eval(sa is a a,s is $ s)

res6:Object = a is 1,s is String`

作为在scala 2.11.6下运行的应用程序的添加示例:

  import javax.script.ScriptEngineManager 

对象EvalTest {
def main(args:Array [String]){
val e = new ScriptEngineManager (e.eval(a,1)
e.put(s,String)
println(e.eval sa is a a,s is $ s))
}
}

$ b b

要使此应用程序正常工作,请确保包含库依赖项。

  libraryDependencies + =org .scala-lang%scala-compiler%scalaVersion.value 


I have found an example code for a Scala runtime scripting in answer to Generating a class from string and instantiating it in Scala 2.10, however the code seems to be obsolete for 2.11 - I cannot find any function corresponding to build.setTypeSignature. Even if it worked, the code seems hard to read and follow to me.

How can Scala scripts be compiled and executed in Scala 2.11?

Let us assume I want following:

  • define several variables (names and values)
  • compile script
  • (optional improvement) change variable values
  • execute script

For simplicity consider following example:

I want to define following variables (programmatically, from the code, not from the script text):

val a = 1
val s = "String"

I want a following script to be compiled and on execution a String value "a is 1, s is String" returned from it:

s"a is $a, s is $s"

How should my functions look like?

def setupVariables() = ???

def compile() = ???

def changeVariables() = ???

def execute() : String = ???

解决方案

Scala 2.11 adds a JSR-223 scripting engine. It should give you the functionality you are looking for. Just as a reminder, as with all of these sorts of dynamic things, including the example listed in the description above, you will lose type safety. You can see below that the return type is always Object.

Scala REPL Example:

scala> import javax.script.ScriptEngineManager

import javax.script.ScriptEngineManager


scala> val e = new ScriptEngineManager().getEngineByName("scala")

e: javax.script.ScriptEngine = scala.tools.nsc.interpreter.IMain@566776ad


scala> e.put("a", 1)

a: Object = 1


scala> e.put("s", "String")

s: Object = String


scala> e.eval("""s"a is $a, s is $s"""")

res6: Object = a is 1, s is String`

An addition example as an application running under scala 2.11.6:

import javax.script.ScriptEngineManager

object EvalTest{
  def main(args: Array[String]){
   val e = new ScriptEngineManager().getEngineByName("scala")
   e.put("a", 1)
   e.put("s", "String")
   println(e.eval("""s"a is $a, s is $s""""))
  }
}

For this application to work make sure to include the library dependency.

libraryDependencies +=  "org.scala-lang" % "scala-compiler" % scalaVersion.value

这篇关于Scala脚本在2.11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆