Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of traits that were mixed in the specified class?

Tags:

scala

traits

And more specific example:

abstract trait A
trait B extends A
trait C extends A

How to check what traits that extend trait A (it can be from 0 to many) were mixed in specified class?

like image 334
Jeriho Avatar asked May 11 '10 09:05

Jeriho


2 Answers

How about a hybrid of the other answers

abstract trait A //interested in this one
trait B extends A //and this one
trait C extends A //this one too
trait D //don't care about this one though

val x = new A with B with D
x.getClass.getInterfaces.filter(classOf[A].isAssignableFrom(_))

returns

Array[java.lang.Class[_]] = Array(interface A, interface B)
like image 70
Kevin Wright Avatar answered Nov 15 '22 08:11

Kevin Wright


scala> val x = new A with B with C
x: java.lang.Object with A with B with C = $anon$1@8ea25fa

scala> x.getClass.getInterfaces
res11: Array[java.lang.Class[_]] = Array(interface A, interface B, interface C)
like image 26
Adam Rabung Avatar answered Nov 15 '22 09:11

Adam Rabung