In Scala 2.10 how do I generate a class from string (probably, using the Toolbox api) later to be instantiated with Scala's reflection?
相关问题
- Unusual use of the new keyword
- C# how to invoke a field initializer using reflect
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- Are GetCallingAssembly() and GetExecutingAssembly(
- GRPC: make high-throughput client in Java/Scala
- Setting up multiple test folders in a SBT project
- Why doesn't reflections.getSubTypesOf(Object.c
W.r.t compilation toolboxes can only run expressions = return values, but not resulting classes or files/byte arrays with compilation results.
However it's still possible to achieve what you want, since in Scala it's so easy to go from type level to value level using implicit values:
Edit. In 2.10.0-RC1 some methods of
ToolBox
have been renamed.parseExpr
is now justparse
, andrunExpr
is now calledeval
.Update #1. If you don't need a java.lang.Class and just need to instantiate the compiled class, you can write
new C
directly in the string submitted torunExpr
.Update #2. It is also possible to have
runExpr
use custom mapping from variable names to runtime values. For example:In this example I create a free term that has a value of 2 (the value doesn't have to be a primitive - it can be your custom object) and bind an identifier to it. This value is then used as-is in the code that is compiled and run by a toolbox.
The example uses manual AST assembly, but it's possible to write a function that parses a string, finds out unbound identifiers, looks up values for them in some mapping and then creates corresponding free terms. There's no such function in Scala 2.10.0 though.