{ sealed trait Sealed; case object Foo extends Sealed; case class Bar(s: String) extends Sealed; trait Baz extends Sealed }
import scala.reflect.runtime.universe._
val List(bar, baz, foo) = symbolOf[Sealed].asClass.knownDirectSubclasses.toList
我已尝试过.asClass.primaryConstructor.isStatic
,但如果将密封的特征定义为依赖类型,则无效。
答案 0 :(得分:6)
Symbol#isModuleClass
看起来可以确定该符号是否为object
,而foo.asClass.isClass
可以确定它是否具有大小写修饰符。请注意,如果您在其他类型的符号上使用它,asClass
将抛出异常(方法,术语等)。
对象测试:
scala> bar.isModuleClass // case class
res28: Boolean = false
scala> baz.isModuleClass // trait
res29: Boolean = false
scala> foo.isModuleClass // case object
res30: Boolean = true
对于其他类型:
scala> val a = ""
a: String = ""
scala> symbolOf[a.type].isModuleClass
res34: Boolean = false
case class A(value: String) ; object A { def default = A("") }
scala> symbolOf[A].isModuleClass
res35: Boolean = false
scala> symbolOf[A.type].isModuleClass
res36: Boolean = true
根据API文档和SI-6012 似乎,isModule
也应该有用,但它只对companionSymbol.
案例测试:
scala> bar.asClass.isCaseClass // case class
res44: Boolean = true
scala> baz.asClass.isCaseClass // trait
res45: Boolean = false
scala> foo.asClass.isCaseClass // case object
res46: Boolean = true
对于其他类型(与上述示例相同的定义):
scala> symbolOf[a.type].asClass.isCaseClass // plain singleton object
res47: Boolean = false
scala> symbolOf[A].asClass.isCaseClass // case class
res48: Boolean = true
scala> symbolOf[A.type].asClass.isCaseClass // non-case object
res49: Boolean = false
将它们放在一起:
def isCaseObject(symbol: Symbol): Boolean =
symbol.isModuleClass && symbol.asClass.isCaseClass
scala> isCaseObject(bar)
res50: Boolean = false
scala> isCaseObject(baz)
res51: Boolean = false
scala> isCaseObject(foo)
res52: Boolean = true
scala> isCaseObject(symbolOf[a.type])
res53: Boolean = false
scala> isCaseObject(symbolOf[A])
res54: Boolean = false
scala> isCaseObject(symbolOf[A.type])
res55: Boolean = false