如何在quasiquote中添加注释

时间:2015-09-02 08:24:37

标签: scala comments abstract-syntax-tree scala-quasiquotes reification

我试图为准引号插入类注释,如此

q"""
    package somePackage {
        /**
         * This is a comment
         */
        public class SomeClass {
        }
    }
"""

但它抛出异常

Exception in thread "main" java.lang.IllegalArgumentException: not legal package stat: <type ?>
    at scala.reflect.internal.ReificationSupport$ReificationSupportImpl.mkPackageStat(ReificationSupport.scala:156)
    at scala.reflect.internal.ReificationSupport$ReificationSupportImpl.mkPackageStat(ReificationSupport.scala:11)
    at database.generate.jooq.JooqGenerate.generateDaos(JooqGenerate.scala:65)
    at database.generate.GenerateDao$.apply(GenerateDao.scala:25)
    at Test$.main(Test.scala:7)
    at Test.main(Test.scala)

1 个答案:

答案 0 :(得分:1)

这对我有用:

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> q"""
     | package somePackage {
     | /**
     | * This is a comment
     | */
     | class SomeClass()
     | }
     | """
res0: reflect.runtime.universe.PackageDef =
package somePackage {
  class SomeClass extends scala.AnyRef {
    def <init>() = {
      super.<init>();
      ()
    }
  }
}

scala>

scala>

是否在引号中使用java类,导致问题?

还要检查您正在使用的Scala版本

scala> util.Properties.versionString
res2: String = version 2.11.7

这里的教程:http://docs.scala-lang.org/overviews/quasiquotes/intro.html