IttayD
IttayD

Reputation: 29143

Scala: How to get class of mixin composition?

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?

Upvotes: 10

Views: 3317

Answers (4)

agentcoops
agentcoops

Reputation: 283

I'm not sure exactly what you're trying to do with the composite class, but what you can do is get the list of interfaces implemented and the superclass of a given anonymous class which might suffice. For example:

trait X
trait Y
class Foo
val bar = new Foo with X with Y
val barClass = bar.getClass // class $anon$1
barClass.getInterfaces // Array(interface X, interface Y)
barClass.getSuperclass // class Foo

Upvotes: 0

Geoff Reedy
Geoff Reedy

Reputation: 36041

You can't get a class literal but you can test if an object meets that type in two different ways:

trait X
trait Y

val xy: AnyRef = new X with Y
val zz: AnyRef = new Object with X

xy.isInstanceOf[X with Y] // true
zz.isInstanceOf[X with Y] // false

xy match { case a: X with Y => true; case _ => false} // true
zz match { case a: X with Y => false; case _ => false} // false

It's a lot like this generic declaration in java

public <T extends Comparable<T> & Serializable> T id(T t) { return t; }

That method gets erased to

public Comparable id(Comparable t) { return t; }

However in Java you cannot say xy instanceof (X&Y) but it's really the same as xy instanceof X && xy instanceof Y

Upvotes: 0

retronym
retronym

Reputation: 55028

classOf[X] only works if X corresponds to a physical class. T with U is 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 U is T.

scala> trait T
defined trait T

scala> trait U
defined trait U

scala> manifest[T with U]
res10: Manifest[T with U] = T with U

scala> manifest[T with U].erasure
res11: java.lang.Class[_] = interface T

Here you can see that List[Int] and List[_] have the same erasure:

scala> classOf[List[_]]
res13: java.lang.Class[List[_]] = class scala.collection.immutable.List

scala> classOf[List[Int]]
res14: java.lang.Class[List[Int]] = class scala.collection.immutable.List

scala> classOf[List[Int]] == classOf[List[_]]
res15: Boolean = true

Upvotes: 7

Joa Ebert
Joa Ebert

Reputation: 6715

No it is not possible because "X with Y" is an anonymous definition in your example. This is not the case for "class X extends Z with Y" of course.

Upvotes: 1

Related Questions