用Scala生成代码 [英] Code generation with Scala
本文介绍了用Scala生成代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Scala中使用SBT工具链时,是否可以编写一个任务,该任务将读取项目源代码的特殊部分以在编译时生成scala代码。
When using the SBT toolchain in Scala, is it possible to write a task that will read a special part of the project's source to generate scala-code at compile time.
对此有任何想法或文章/教程吗?我正在寻找与Template Haskell类似的东西。
Any ideas or even articles/tutorials on this? I am looking for something rather similar to Template Haskell.
推荐答案
treehugger.scala 是一个用于代码生成的库。
treehugger.scala is a library designed for code generation.
import treehugger.forest._
import definitions._
import treehuggerDSL._
val tree: Tree = Predef_println APPLY LIT("Hello, world!")
println(tree)
println(treeToString(tree))
上面的代码显示两个行:
The above code prints two lines:
Apply(Ident(println),List(Literal(Constant(Hello, world!))))
println("Hello, world!")
treehugger确实会生成AST,但不符合scalac的AST。
treehugger does generate an AST, but non-compliant to scalac's AST.
这篇关于用Scala生成代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文