reflection - How to display all types of an object (in Scala)? -
with isinstanceof method, 1 can check type of object. example:
scala> val i: int = 5 i: int = 5 scala> val a: = a: = 5 scala> a.isinstanceof[any] res0: boolean = true scala> a.isinstanceof[int] res1: boolean = true scala> a.isinstanceof[string] res2: boolean = false how can 1 display types of object (if possible @ ?) ?
you can pretty in 2.10 (m4 or later):
import scala.reflect.runtime.universe._ def supertypes(t: type): set[type] = (t.parents ++ t.parents.flatmap(supertypes)).toset def alltypes[a](a: a)(implicit tag: typetag[a]) = supertypes(tag.tpe) + tag.tpe which gives following:
scala> alltypes(1).foreach(println) anyval notnull int scala> alltypes("1").foreach(println) string object comparable[string] charsequence java.io.serializable scala> alltypes(list("1")).foreach(println) scala.collection.linearseq[string] scala.collection.genseq[string] scala.collection.iterablelike[string,list[string]] scala.collection.geniterable[string] scala.collection.gentraversablelike[string,iterable[string]] ... you'll have harder time trying pre-2.10.
Comments
Post a Comment