これはTypeTags(少なくとも2.10M7)で達成することができます:
scala> class Foo; trait Bar
defined class Foo
defined trait Bar
scala> import reflect.runtime.universe._
import reflect.runtime.universe._
scala> def isBar[A <: Foo : TypeTag] = typeOf[A].baseClasses.contains(typeOf[Bar].typeSymbol)
isBar: [A <: Foo](implicit evidence$1: reflect.runtime.universe.TypeTag[A])Boolean
scala> isBar[Foo]
res43: Boolean = false
scala> isBar[Foo with Bar]
res44: Boolean = true
TypeTagsは1を提供しています。したがって、彼らははるかに強力な昔ながらのマニフェストよりも、次のとおりです。メソッドtpe
で
scala> val fooBar = typeTag[Foo with Bar]
fooBar: reflect.runtime.universe.TypeTag[Foo with Bar] = TypeTag[Foo with Bar]
我々はScalas新しいリフレクションへのフルアクセスを取得:
scala> val tpe = fooBar.tpe // equivalent to typeOf[Foo with Bar]
tpe: reflect.runtime.universe.Type = Foo with Bar
scala> val tpe.<tab><tab> // lot of nice methods here
=:= asInstanceOf asSeenFrom baseClasses baseType contains declaration
declarations erasure exists find foreach isInstanceOf kind
map member members narrow normalize substituteSymbols substituteTypes
takesTypeArgs termSymbol toString typeConstructor typeSymbol widen
'typeTag [Foo with Bar]'は '暗黙的に[TypeTag [Foo with Bar]]'(<2.10の 'Predef.manifest'によく似ています)に便利です。 –
ショートカットについて言えば、 'typeOf [T]'は 'typeTag [T] .tpe'と同じです。 –