【问题标题】:Get class that is being refined from the refinement从细化中获取正在细化的类
【发布时间】:2021-01-14 09:03:22
【问题描述】:
考虑以下sealed trait:
sealed trait Type
object Type {
case object S
}
sealed trait Test{
type Tpe <: Type
}
object Test {
type Aux[T <: Type] = Test{ type Tpe = T }
}
给定一个表示Test.Aux[S.type] 的ClassSymbol 有没有办法获得一个表示sealed trait Test 的ClassSymbol?
【问题讨论】:
标签:
scala
reflection
metaprogramming
scala-macros
aux-pattern
【解决方案1】:
在挖掘 API 后,我找到了以下解决方案:
def refinedSealedTrait(symbol: Symbols#Symbol): Symbol = {
if (!symbol.isRefinementClass) {
return false
}
val parents = symbol.parentSymbols
if (parents.size != 1) {
return false
}
val parentSymbol = parents.iterator.next
if (parentSymbol.isSealed && parentSymbol.isTrait) {
parentSymbol
} else {
c.abort(...)
}
}