score:2

Accepted answer

trait in scala translate to interfaces in java, so you can use java's reflection library to find out which traits are implemented. here is an example:

trait foo
class bar extends foo 

val b = new bar
b.getclass.getinterfaces.foreach(println)

this prints:

interface foo
interface scala.scalaobject

score:0

note that the example kim stebel used does not work if the trait is implemented by a superclass. here is a more general form:

  def implementsinterface(target: class[_], someinterface: class[_]): boolean = {
    val i = target.getinterfaces
    i.foreach((c: class[_]) => if (c == someinterface) return true)

    val s = target.getsuperclass

    if (s == null) false
    else implementsinterface(s, someinterface)
  }

Related Query

More Query from same tag