What are the reasons for the differences between compile time types and runtime types?

In Scala / Java (~ on the JVM), there are several smaller cases where the behavior is different from:

/* "Dynamic" cast */
"".getClass.cast("Foo")
//res0: Any = Foo

/* "Static" cast */
classOf[String].cast("Foo")
//res1: String = Foo

/* "Compile time cast" */
"Foo".asInstanceOf[String]
res2: String = Foo

In which languages ​​is the gap between compilation time and runtime greater, and are there any reasons from the POV language, why can this be a "good thing"?

Moving in the other direction: is there a (statically typed) language without any distinction between compilation time and runtime?

+5
source share
2 answers

The reason for the first result is that the method getClasshas the following signature in Java

public final Class<?> getClass()

Scala. , getClass T,

public final Class<? extends T> getClass()

. , ,

public final Class<? extends Receiver> getClass()

- getClass. , Snoracle Java getClass, , Java. , - T, , T.class (java) classOf[T] (scala). , , .

" " "" , . desugar x.asInstanceOf[T] to classOf[T].cast(x).

, , , . , , , , . , , JVM.

, .

+6

C (RTTI). , - . ( ). .

++ RTTI / , - ( ++).

Java ( ) ( 7 ) . , . , , Java Object . , ( ). , (C, Pascal) (void *) (++, Haskell).

Haskell , . GHC - ( ??), . , - runtme.

+1

All Articles