在Scala 2.10

时间:2016-09-26 22:08:39

标签: scala macros

为了向后兼容,我试图在Scala 2.10中使用一个简单的宏,但是在2.10中,类型绑定被完全忽略。

implicit def createInstance[T <: Enumeration]: TypeClass[T#Value] = macro TypeClassMacro.enumMaterializer[T]

enumMaterializer还会重新强制执行类型绑定:

  def enumMaterializer[T <: Enumeration : c.WeakTypeTag](
    c: CrossVersionContext
  ): c.Expr[TypeClass[T#Value]] = {
    import c.universe._
    val tpe = weakTypeOf[T]
    val companion = tpe.typeSymbol.companion
    // .....
  }

这是一个白盒宏,它的价值,它在Scala 2.11中完美编译。然而,在2.10中,宏调用是不明确的。我在范围内定义了TypeClass的其他实例,这些实例是手工定义的。

此处使用宏扩展来为TypeClass的包装器派生类型类,例如Option[TypeClass]List[TypeClass]或上述情况Enumeration

但是编译器认为隐含是模棱两可的,因为它期望TypeClass[String]=:= TypeClass[Enum]。不确定这是如何发生的,因为宏的类型输出明确指的是T#Value,这是枚举的内部类型。

ambiguous implicit values:
[error]  both macro method enumTypeClass in object TypeClass of type [T <: Enumeration]=> com.bla.TypeClass[T#Value]
[error]  and object StringTypeClass in object TypeClass of type com.bla.TypeClass.StringTypeClass.type

我正在使用Scala 2.10.6,到目前为止只能假设这是一个错误。

0 个答案:

没有答案