Code generation with Scala

前端 未结 3 1283
时光取名叫无心
时光取名叫无心 2021-02-13 16:44

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.

Any

3条回答
  •  隐瞒了意图╮
    2021-02-13 17:02

    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 does generate an AST, but non-compliant to scalac's AST.

提交回复
热议问题