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)
}
Source: stackoverflow.com
Related Query
- Introspect a Scala class for traits?
- Scala client composition with Traits vs implementing an abstract class
- How to write copy() method for Simple Class in Scala
- Understanding Case class and Traits in Scala
- How to access a Java static method from Scala given a type alias for that class it resides in
- In scala is it possible for a trait to extend a class which needs parameters?
- Scala spark: how to use dataset for a case class with the schema has snake_case?
- Select certain super class for method call in Scala
- scala Duration: "This class is not meant as a general purpose representation of time, it is optimized for the needs of scala.concurrent."
- Writing type class instances for nested classes in Scala
- About Scala generics: cannot find class manifest for element type T
- Scala Dynamic Parse Json using case class No Manifest available for T
- Is there any Scala built-in class for capturing an external process's output?
- Partial EMMA code coverage in Scala Case Class for IntelliJ IDEA 10.5
- Can you get a class name as a constant for scala annotations?
- scala generic encoder for spark case class
- java.lang.NoSuchMethodException for init method in Scala case class
- What is actuall class for structural type scala syntax definition?
- Scala style for empty class parameter lists
- How do I use Scala @specialized for a memory-efficient wrapper class around a primitive?
- Scala value class compilation fails for base type with partial-function-parameter method
- Best way to perform case insensitive comparison for Scala case class
- How Scala achieve performance improvement for Map and Set by using different Class based on size?
- == for case class and "non-case" class in Scala
- How to find scala api documentation for string class
- How do I write a scala extractor for a case class with default parameters?
- scala circe encoders/decoders for an abstract class with case classes
- Scala type class for obtaining the most concrete type for a type member
- Clojure interop for Scala objects and traits
- How to define cast and implicit cast operations for a Scala class of mine?
More Query from same tag
- Dropping duplicate records based using window function in spark scala
- Attach kafka offset to each record in foreachRDD
- How to have (Array[Int](9), Int) as key for Map?
- How to check if a scala class is assignable from a java class
- Can not download jars when building scala project with sbteclipse?
- Merging RDDs using Scala Apache Spark
- Compilation errors with spark cassandra connector and SBT
- Filtering inactivated rows in Spark using Scala
- Scala Generic inference
- Suggest imports does not work with Scala IDE (and sbt)
- How to extract each JSONobject from JSONArray and save to cassandra in spark streaming
- How to model a relation in Scala between the key type and the corresponding value type
- Custom ETag algorithm for asset fingerprinting
- Debugging a software verifier written in sbt on Intellij Idea
- How to create a Scala executable jar file that is built with Maven, and has log4j included, using IntelliJ IDEA IDE
- Scala reduce on Case class with keys
- Spark: driver logs showing "thread spilling sort data to disk"
- Use FIRRTL Annotations to connect multi-bit wires and pins
- Allow all requests to the AKKA-HTTP service
- Exception when calling collect() function in scala
- Apache Spark RDD Split "|"
- How to break REST controller that return Action in Scala/Play framework
- Get instance of singleton type in scala
- Running future n times
- Reversing a Map but using the values as keys, and keys as values
- How exactly sbt figures out task names?
- Is connection pooling inherently done in cloudant database?
- Optimizing list processing in Scala
- Scala: Pattern match on type but exclude specific subtypes?
- Await statement execution completion in Slick