如何检查某些类型是否符合 Scala 宏中的泛型类型边界?
How to check if some type confirms to generic type bounds in Scala macros?
我在一些class中有以下方法:
def writeGeneric[G <: GenericTrait](value: G)
我正在使用 Scala 宏处理该方法。此方法 m.typeParams
的提取类型参数打印为 type G <: GenericTrait
。
其中 m
是 MethodSymbol
.
如何测试其他类型是否符合此类型的界限?
例如,这不起作用:
typeOf[OtherType] <:< m.typeParams.head.typeSignature
我找到了手动边界检查的解决方案:
val typeParamSymbol = m.typeParams.head
val other = typeOf[OtherType]
val typeMatched = typeParamSymbol.typeSignature match {
case TypeBounds(lo,hi) ⇒
lo <:< other && other <:< hi
case other: Type ⇒
left <:< other
}
仍然希望有更好的解决方案。
我在一些class中有以下方法:
def writeGeneric[G <: GenericTrait](value: G)
我正在使用 Scala 宏处理该方法。此方法 m.typeParams
的提取类型参数打印为 type G <: GenericTrait
。
其中 m
是 MethodSymbol
.
如何测试其他类型是否符合此类型的界限? 例如,这不起作用:
typeOf[OtherType] <:< m.typeParams.head.typeSignature
我找到了手动边界检查的解决方案:
val typeParamSymbol = m.typeParams.head
val other = typeOf[OtherType]
val typeMatched = typeParamSymbol.typeSignature match {
case TypeBounds(lo,hi) ⇒
lo <:< other && other <:< hi
case other: Type ⇒
left <:< other
}
仍然希望有更好的解决方案。