Dependent type seems to “not work” when generated by Scala macro

╄→尐↘猪︶ㄣ 提交于 2019-12-05 18:27:17

Nothing's wrong with your macro, probably. It's the type signatures:

def apply[T](struct: T): Flattened[T] = macro impl[T]
def impl[T : c.WeakTypeTag](c: Context)(struct: c.Tree): c.Tree

You are using a blackbox macro, and, according to the documentation, blackbox macros are true to their signatures. That is, even though impl produces a Flattened[T] { type U = ... }, the fact that it is a blackbox macro means that scalac always wraps it in (_: Flattened[T]), forgetting the definition of U in the refinement. Make it a whitebox macro:

// import scala.reflect.macros.blackbox.context // NO!
import scala.reflect.macros.whitebox.context
def impl[T: c.WeakTypeTag](c: Context)(struct: c.Tree): c.Tree
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!