如何将宏注释应用于具有上下文绑定的案例类? [英] How can I apply a macro annotation to a case class with a context bound?

查看:84
本文介绍了如何将宏注释应用于具有上下文绑定的案例类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试向我的案例类添加宏注释时:

When I try to add a macro annotation to my case class:

@macid case class CC[A: T](val x: A)

我收到错误:

private[this] not allowed for case class parameters

@macid 只是恒等函数,定义为一个白盒StaticAnnotation:

@macid is just the identity function, defined as a whitebox 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)

如果我删除上下文绑定,它会起作用:

It works if I remove the context bound:

@macid case class CC[A](val x: A)

发生的事情是上下文绑定被脱糖为私有参数.以下脱糖代码得到相同的错误:

What's happening is the context bound is desugared into a private parameter. The following desugared code gets the same error:

@macid case class CC[A](val x: A)(implicit aIsT: T[A])

为了获得工作代码,我使用 val 公开隐式参数:

To get working code I make the implicit parameter public with val:

@macid case class CC[A](val x: A)(implicit val aIsT: T[A])

所以我的问题是:宏注释支持上下文边界的正确方法是什么?为什么编译器会对宏注解生成的代码进行 no-private-parameters-of-case-classes 检查,而对普通代码不进行检查?

So my questions are: What's the right way for a macro annotation to support context bounds? Why does the compiler perform a no-private-parameters-of-case-classes check for code that's generated by a macro annotation, but doesn't perform the check for ordinary code?

Scala 版本 2.11.7 和 2.12.0-M3 都报告错误.以上所有代码示例在 2.11.3 中都按预期编译和运行.

Scala versions 2.11.7 and 2.12.0-M3 both report the error. All the above code examples compile and run as expected in 2.11.3.

推荐答案

似乎是一个错误.这是宏看到的树:

Seems like a bug. Here is the tree as seen by the macro:

case class CC[A] extends scala.Product with scala.Serializable {
  <caseaccessor> <paramaccessor> val x: A = _;
  implicit <synthetic> <caseaccessor> <paramaccessor> private[this] val evidence$1: T[A] = _;
  def <init>(x: A)(implicit evidence$1: T[A]) = {
    super.<init>();
    ()
  }
}

并通过运行时反射 API:

And through the runtime reflection API:

case class CC[A] extends Product with Serializable {
  <caseaccessor> <paramaccessor> val x: A = _;
  implicit <synthetic> <paramaccessor> private[this] val evidence$1: $read.T[A] = _;
  def <init>(x: A)(implicit evidence$1: $read.T[A]) = {
    super.<init>();
    ()
  }
};

前者在 evidence$1 上有一个额外的 标志,但它不应该这样做.似乎案例类的所有隐式参数都被错误地赋予了这个标志.

The former has an extra <caseaccessor> flag on evidence$1 when it should not. It seems as though all implicit parameters to case classes are mistakenly given this flag.

这篇关于如何将宏注释应用于具有上下文绑定的案例类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆