scala> import java.util.Properties
import java.util.Properties
scala> trait Foo extends Properties
defined trait Foo
scala> classOf[Foo]
res0: java.lang.Class[Foo] = interface Foo
scala> class FooP extends Foo
defined class FooP
scala> classOf[FooP]
res1: java.lang.Class[FooP] = class FooP
scala> classOf[Properties with Foo]
<console>:7: error: class type required but java.util.Properties with Foo found
classOf[Properties with Foo]
^
scala> new Properties with Foo
res2: java.util.Properties with Foo = {}
scala> res2.getClass
res3: java.lang.Class[_] = class $anon$1
Is there a way of getting class of ‘Properties with Foo’ without creating an instance or new class?
classOf[X]only works ifXcorresponds to a physical class.T with Uis a compound type, and doesn’t correspond to a class.You can use Manifests to determine the erasure of a type. The type erasure of
T with UisT.Here you can see that
List[Int]andList[_]have the same erasure: