Something to underline in Seth’s latter example: AFAIK, <:< is mainly used in type signatures, to prove that a type relationship is correct at compile time. I hadn’t even realized it could be used at runtime…
That output is as-expected, because Type and TypeTag represent compile-time types, but as soon as you List(1,2.0,"three"), you’ve told the compiler to throw away its compile-time knowledge of the types of the individual items and just treat all of them as Anys.
If you look at Dmytro’s original post, he already said so:
Notice that data has type List[Any] so both c1 and c2 are Any
What is it that you’re actually trying to accomplish?
I’m trying to understand what dynamic type information is available on data which the compiler thinks is Any. What information can be attained at runtime even if the compiler doesn’t know much information about the types.
Answer 1: The question suggests confused thinking. What are you trying to accomplish?
Answer 2: Yes:
scala 2.13.3> class C; val x: Any = new C
val x: Any = C@6740a11c
scala 2.13.3> import scala.reflect.runtime.universe._
scala 2.13.3> val mirror = runtimeMirror(getClass.getClassLoader)
val mirror: reflect.runtime.universe.Mirror = ...
scala 2.13.3> mirror.classSymbol(x.getClass).toType
val res0: reflect.runtime.universe.Type = C
Taking a look at the blog post, it is a bit confusing.
In the section * No values, infinite types: method type parameters* the author seems to reach an invalid conclusion from this though experiment. He says assume you can list all the classes in your class path, then reaches a conclusion from which he concludes that G is not a class. That’s a wrong conclusion. Isn’t it? It could also mean that the set of classes is uncountable.
I would guess that the set of types is uncountable, at least it is in the type systems I know more about.
Seth, I’m not sure if you’re really interesting in knowing the details of what I’m trying to accomplish. I’m happy to answer, but I don’t want to bore or burden you excessively.
I’m mainly trying to understand what dynamic capability Scala presents to the programming. When I have a heterogeneous collection of objects, what kind of type/class queries I can do at run-time. I.e., can I search for particular type patterns in such data.
To which extent does the runtime type/class reflection interface differ from that of clojure which is implemented atop the same JVM.
The ultimate goal is to try to describe how much of this type of dynamic type checking can be done in Scala and Clojure atop the Java type system.