【问题标题】: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 TestClassSymbol

【问题讨论】:

    标签: 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(...)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 2018-03-15
      • 1970-01-01
      • 2010-10-26
      • 2019-04-03
      • 1970-01-01
      相关资源
      最近更新 更多