Scala macro to print code?

后端 未结 3 1667
面向向阳花
面向向阳花 2020-12-18 04:23

I want to do something like this:

def assuming[A](condition: => Boolean)(f: => A): A = {
  require(condition, /* print source-code of condition */)
  f         


        
3条回答
  •  醉酒成梦
    2020-12-18 04:46

    Have you consulted the docs at:

    http://www.scala-lang.org/api/2.11.0/scala-reflect/#scala.reflect.api.Printers

    scala> show(q"-1 < 0")
    res6: String = -1.$less(0)
    
    scala> showCode(q"-1 < 0")
    res7: String = (-1).<(0)
    

    Alternatively, folks have used source positions to glean snippets to print.

提交回复
热议问题