将字符串转换为可运行代码的方法有哪些? [英] What are the ways to convert a String into runnable code?

查看:38
本文介绍了将字符串转换为可运行代码的方法有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到如何将字符串转换为可运行代码的方法,例如:

I could not find how to convert a String into runnable code, for instance:

val i = "new String('Yo')"
// conversion
println(i)

应该打印

Yo

转换后.

我在另一篇文章中找到了以下示例:

I found the following example in another post:

import scala.tools.nsc.interpreter.ILoop
import java.io.StringReader
import java.io.StringWriter
import java.io.PrintWriter
import java.io.BufferedReader
import scala.tools.nsc.Settings

object FuncRunner extends App {

  val line = "sin(2 * Pi * 400 * t)"

  val lines = """import scala.math._
    |var t = 1""".stripMargin

  val in = new StringReader(lines + "\n" + line + "\nval f = (t: Int) => " + line)
  val out = new StringWriter

  val settings = new Settings

  val looper = new ILoop(new BufferedReader(in), new PrintWriter(out))
  val res = looper process settings
  Console println s"[$res] $out"
}

链接:如何将字符串从文本输入转换为 Scala 中的函数

但似乎 scala.tools 不再可用,而且我是 Scala 的新手,所以我不知道如何替换它.也许现在还有其他方法可以做到这一点.

But it seems like scala.tools is not available anymore, and I'm a newbie in Scala so i could not figure out how to replace it. And may be there are just other ways to do it now.

谢谢!

推荐答案

您可以使用 Quasiquotes(实验模块)简单地执行包含在 String 中的代码.

You can simple execute your code contained inside String using Quasiquotes(Experimental Module).

import scala.reflect.runtime.universe._
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox

// TO compile and run code we will use a ToolBox api.
val toolbox = currentMirror.mkToolBox()

// write your code starting with q  and put it inside double quotes.
// NOTE : you will have to use triple quotes if you have any double quotes usage in your code.
val code1 = q"""new String("hello")"""
//compile and run your code.
val result1 = toolbox.compile(code1)()

// another example
 val code2 = q"""
 case class A(name:String,age:Int){
    def f = (name,age)
 }
 val a = new A("Your Name",22)
 a.f
 """

 val result2 = toolbox.compile(code2)()

REPL 输出:

// Exiting paste mode, now interpreting.

import scala.reflect.runtime.universe._
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
toolbox: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@69b34f89
code1: reflect.runtime.universe.Tree = new String("hello")
result1: Any = hello
code2: reflect.runtime.universe.Tree =
{
  case class A extends scala.Product with scala.Serializable {
    <caseaccessor> <paramaccessor> val name: String = _;
    <caseaccessor> <paramaccessor> val age: Int = _;
    def <init>(name: String, age: Int) = {
      super.<init>();
      ()
    };
    def f = scala.Tuple2(name, age)
  };
  val a = new A("Your Name", 22);
  a.f
}
result2: Any = (Your Name,22)

scala> 

要了解有关 Quasiquotes 的更多信息:http://docs.scala-lang.org/overviews/quasiquotes/setup.html

To learn more about Quasiquotes : http://docs.scala-lang.org/overviews/quasiquotes/setup.html

这篇关于将字符串转换为可运行代码的方法有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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