如何将宏注释应用于具有上下文绑定的案例 class?
How can I apply a macro annotation to a case class with a context bound?
当我尝试向我的案例添加宏注释时 class:
@macid case class CC[A: T](val x: A)
我收到错误:
private[this] not allowed for case class parameters
@macid
只是恒等函数,定义为白盒 StaticAnnotation:
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
import scala.annotation.StaticAnnotation
class macid extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro macidMacro.impl
}
object macidMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
new Macros[c.type](c).macidMacroImpl(annottees.toList)
}
}
class Macros[C <: Context](val c: C) {
import c.universe._
def macidMacroImpl(annottees: List[c.Expr[Any]]): c.Expr[Any] =
annottees(0)
}
未注释的代码有效:
case class CC[A: T](val x: A)
如果我删除上下文绑定,它会起作用:
@macid case class CC[A](val x: A)
发生的事情是上下文绑定被脱糖为私有参数。以下脱糖代码得到相同的错误:
@macid case class CC[A](val x: A)(implicit aIsT: T[A])
为了获得工作代码,我将隐式参数 public 设置为 val
:
@macid case class CC[A](val x: A)(implicit val aIsT: T[A])
所以我的问题是:宏注释支持上下文边界的正确方法是什么?为什么编译器对宏注解生成的代码执行 no-private-parameters-of-case-classes 检查,但不对普通代码执行检查?
Scala版本2.11.7和2.12.0-M3均报错。以上所有代码示例均按 2.11.3.
中的预期进行编译和 运行
似乎是一个错误。这是宏看到的树:
case class CC[A] extends scala.Product with scala.Serializable {
<caseaccessor> <paramaccessor> val x: A = _;
implicit <synthetic> <caseaccessor> <paramaccessor> private[this] val evidence: T[A] = _;
def <init>(x: A)(implicit evidence: T[A]) = {
super.<init>();
()
}
}
并通过运行时反射API:
case class CC[A] extends Product with Serializable {
<caseaccessor> <paramaccessor> val x: A = _;
implicit <synthetic> <paramaccessor> private[this] val evidence: $read.T[A] = _;
def <init>(x: A)(implicit evidence: $read.T[A]) = {
super.<init>();
()
}
};
前者在 evidence
上有一个额外的 <caseaccessor>
标志,但它不应该。似乎 case 类 的所有隐式参数都被错误地赋予了这个标志。
当我尝试向我的案例添加宏注释时 class:
@macid case class CC[A: T](val x: A)
我收到错误:
private[this] not allowed for case class parameters
@macid
只是恒等函数,定义为白盒 StaticAnnotation:
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
import scala.annotation.StaticAnnotation
class macid extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro macidMacro.impl
}
object macidMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
new Macros[c.type](c).macidMacroImpl(annottees.toList)
}
}
class Macros[C <: Context](val c: C) {
import c.universe._
def macidMacroImpl(annottees: List[c.Expr[Any]]): c.Expr[Any] =
annottees(0)
}
未注释的代码有效:
case class CC[A: T](val x: A)
如果我删除上下文绑定,它会起作用:
@macid case class CC[A](val x: A)
发生的事情是上下文绑定被脱糖为私有参数。以下脱糖代码得到相同的错误:
@macid case class CC[A](val x: A)(implicit aIsT: T[A])
为了获得工作代码,我将隐式参数 public 设置为 val
:
@macid case class CC[A](val x: A)(implicit val aIsT: T[A])
所以我的问题是:宏注释支持上下文边界的正确方法是什么?为什么编译器对宏注解生成的代码执行 no-private-parameters-of-case-classes 检查,但不对普通代码执行检查?
Scala版本2.11.7和2.12.0-M3均报错。以上所有代码示例均按 2.11.3.
中的预期进行编译和 运行似乎是一个错误。这是宏看到的树:
case class CC[A] extends scala.Product with scala.Serializable {
<caseaccessor> <paramaccessor> val x: A = _;
implicit <synthetic> <caseaccessor> <paramaccessor> private[this] val evidence: T[A] = _;
def <init>(x: A)(implicit evidence: T[A]) = {
super.<init>();
()
}
}
并通过运行时反射API:
case class CC[A] extends Product with Serializable {
<caseaccessor> <paramaccessor> val x: A = _;
implicit <synthetic> <paramaccessor> private[this] val evidence: $read.T[A] = _;
def <init>(x: A)(implicit evidence: $read.T[A]) = {
super.<init>();
()
}
};
前者在 evidence
上有一个额外的 <caseaccessor>
标志,但它不应该。似乎 case 类 的所有隐式参数都被错误地赋予了这个标志。