Scala, Maven, and preprocessors

前端 未结 4 468
梦毁少年i
梦毁少年i 2021-01-20 00:46

I know all of the philosophical arguments against preprocessors and macros in Java. I don\'t agree that just because some may abuse a language feature, it should be excluded

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-20 01:25

    Update @ralph Well, this is a question from 2+ years ago, but since I'm having the same need for __LINE__ and __FILE__ and you had mentioned SCALA; here are some ideas I'm having, using Scala macros (as of v2.10.0-RC1)

    def $currentPosition:String = macro _currentPosition;
    def _currentPosition(c:Context):c.Expr[String]={ import c.universe._;
      val pos = c.enclosingPosition;
      c.Expr(Literal(Constant(
        s"${pos.source.path}: line ${pos.line}, column ${pos.column}" )))
    }
    

    Macros being evaluated at compile-time, $currentPosition is replaced with a literal string that describes its position in the source code. For example, put in a println at line 13, it displays:

    /sandbox/tmp_juno_workspace2/LogMacro_Test/src/test/Trial.scala: line 13, column 15
    

    I have not played with these mechanisms extensively, but by tweaking the thing, one can develop the logging features (s)he requires (I should add that writing macros can be difficult - it is for me!).

提交回复
热议问题