mardi 5 janvier 2016

dynamically parse a string and return a function in scala using reflection and interpretors

I am trying to dinamically interpret code given as a String. Eg:

val myString = "def f(x:Int):Int=x+1".

Im looking for a method that will return the real function out of it: Eg:

val myIncrementFunction = myDarkMagicFunctionThatWillBuildMyFunction(myString)
println(myIncrementFunction(3))

will print 4

Use case: I want to use some simple functions from that interpreted code later in my code. For example they can provide something like def fun(x: Int): Int = x + 1 as a string, then I use the interpreter to compile/execute that code and then I'd like to be able to use this fun(x) in a map for example.

The problem is that that function type is unknown for me, and this is one of the big problems because I need to cast back from IMain. I've read about reflection, type system and such, and after some googling I reached this point. Also I checked twitter's util-eval but I cant see too much from the docs and the examples in their tests, it's pretty the same thing.

If I know the type I can do something like

val settings = new Settings
val imain = new IMain(settings)
val res = imain.interpret("def f(x:Int):Int=x+1; val ret=f _ ")
val myF = imain.valueOfTerm("ret").get.asInstanceOf[Function[Int,Int]]
println(myF(2))

which works correctly and prints 3 but I am blocked by the problem I said above, that I dont know the type of the function, and this example works just because I casted to the type I used when I defined the string function for testing how IMain works.

Do you know any method how I could achieve this functionality ?

I'm a newbie so please excuse me if I wrote any mistakes.

Thanks





Aucun commentaire:

Enregistrer un commentaire