Kotlin: Reified Type Function Parameters

As most Java programmers know, Java generics are erased at compile-time. This has trade-offs, but the two main reasons for this are:

  • Compatibility - Java 1.4 and earlier dealt exclusively in raw types at the VM level. Generics were able to compile without introducing significant new typing to the bytecode and classfile format spec, and having to deal with older classes generated without that typing.
  • Simplicity - By erasing to raw types, the JVM doesn’t have to understand specialization; something that has its own complexities and downsides. For example, specialized types are much more challenging to optimize with a just-in-time compiler.

However, knowing the type parameters used at runtime can have real value, and it’s something Java simply doesn’t offer. Kotlin would like to help in this area, but there are many challenges in doing so.

Because the classfile format and runtime do not understand type parameterizers, that means that languages like Kotlin and Scala (which must eventually target the JVM classfile format) also must consider the consequences of erasure.

JVM Languages can choose to do things like generate specialized classes at compile time or create metadata payloads within classes and instances, but generally, Kotlin and Scala will erase their types as well to boil down to raw classes because they want to maximize opportunity for Java language interoperability. Kotlin’s specification is generally pretty clear about how it translates to Java classes (aka how it “interops”), and most of it is fairly unsurprising. Having a bunch of hidden variables in the code to reflect the types used at a particular point in time would make Java interop awful.

However, sometimes having “reified” type information (resolved type parameter metadata) at runtime is extremely valuable; this is something that Java simply doesn’t have, and consequently you will see methods in Java that look like this:

 1// Some type that is parameterized but also needs to know the type at runtime.
 2public class MyThing<T> { // compile time type.
 4  // Factory method *must* take the runtime type if it is to be used at runtime.
 5  public static MyThing<T> create(Class<T> type) {
 6    return new MyThing<T>(type);
 7  }
 9  private Class<T> type; // Capture of type at runtime.
11  // Required type via constructor.
12  public MyThing(Class<T> type) {
13    this.type = type;
14  }
16  public void printType() {
17    // Runtime use of that type.
18    System.out.println(type);
19  }
22// ... use:
24MyThing<String> result = MyThing.create(String.class);

You often see this sort of pattern in factories, database libraries, and serializer/deserializer frameworks; places where the framework uses parameters for organizing instances of a tool, but the underlying tool must also know the type at runtime to be able to walk the fields or similar RTTI operations (e.g. Dao<Person> vs Dao<BankAccount>).

In Kotlin we can model this same class and same factory method, effectively just porting the Java implementation naively:

 1class MyThing<T>(private val type: Class<T>) {
 2  fun printType() = println(type)
 4  companion object {
 5    fun create(Class<T> type) = MyThing<T>(type)
 6  }
 9// ... use:
10val result = MyThing.create(String::class.java)

However, Kotlin also supports a limited form of reification, meaning that it can carry and track the type parameter for you in code.

Here is an update of this class using reified parameters:

 1class MyThing<T>(private val type: Class<T>) {
 2  fun printType() = println(type)
 4  companion object {
 5    inline fun <reified T> create(Class<T> type) = MyThing<T>(T::class.java)
 6  }
 9// ... use:
10val result = MyThing.create<String>()

Note that we refer to T in the create method, as if it was a fully resolved type literal. Specifically you can use:

  • is comparators (x is T, x !is T)
  • as casts (x as T, x as? T)
  • Reflection literals like ::class

This functionality has some limitations, however:

  • It only can be used as parameters to inline functions
  • The type information is reduced to runtime-available reification (meaning that there is no encoding of nested type parameters ala List<String> as opposed to List<*>)
  • Primitive types are translated into their boxed forms

Behind the Scenes

These restrictions may seem odd, but the reason is quite clever.

By restricting this feature to inline functions, the Kotlin compiler can simply compile the function such that every place where T is referenced the literal type from the use-site is put in its place in the code. In this way, inline functions provide a way to do “use-site type specialization” in the code because every place the code is “inlined” the type information is simply encoded in the inlined code, instead of trying to build a type parameterization solution.

So in the code above, the raw Java bytecode translation would look like this were it in Java source (or was decompiled):

1// Kotlin:
2val result = MyThing.create<String>()
4// --> in Java code:
5MyThing<String> result = new MyThing<String>(String.class);

Because it is inline, this function is entirely invisible to the actual bytecode; it’s purely there as a code organization tool.

The other major advantage to forcing this only on inline functions is it effectively punts any question of Java type interoperability. An area where Scala often must create unpleasant edges when it comes to the generated Java classes (and in turn impacting Java interop) is with respect to the sheer number of synthetics and hidden encoded details hidden in the actual Java classes it can generate. Kotlin is able to avoid this by not diverging as far as Scala when it comes to the power of the type system (and other areas).

You can imagine if you had to do this with some sort of non-inline function it would require hidden type parameters in the code. Something like this:

1public static MyThing create(Class __superSecretHiddenType) {
2  return new MyThing(__superSecretHiddenType);

And, now you can also see why Java interop would suffer, as this method would exist, but it has unexpected type parameters. Instead, inline functions simply don’t exist.

For another example, let’s look at this Kotlin sample:

 1fun main(args: Array<String>) {
 2    myFun<String>("test")
 3    myFun<String>(123)
 6inline fun <reified T> myFun(any: Any) {
 7    if(any is T) {
 8        println("$any matches ${T::class}")
 9    } else {
10        println("$any does not match ${T::class}")
11    }

If you were to run this, it would print this:

1test matches class kotlin.String
2123 does not match class kotlin.String

By looking at the corresponding bytecode, we can see the result of this function were it written in Java is effectively this:

 1// I've taken some liberties by naming some ldcs for readability.
 2public static void main(String[] args) {
 3  String c1 = "test";
 4    String str = new StringBuilder(c1)
 5      .append(" matches ")
 6      .append(kotlin.jvm.internal.Reflection.getOrCreateKotlinClass(java.lang.String));
 7    System.out.println(str);
10  Integer c2 = Integer.valueOf(123);
12  if(c2 instanceof java.lang.String) {
13    String str = new StringBuilder(c2)
14      .append(" matches ")
15      .append(kotlin.jvm.internal.Reflection.getOrCreateKotlinClass(java.lang.String));
16    System.out.println(str);
17  } else {
18    String str = new StringBuilder(c2)
19      .append(" does not match ")
20      .append(kotlin.jvm.internal.Reflection.getOrCreateClass(java.lang.Integer.Class));
21    System.out.println(str);
22  }

Wow! What happened here? Well it turns out that the compilation process was able to tell that the first condition (is a String a String) would always result in true, so it eliminated the branch entirely before emitting the code. This is another case where the compiler can take liberties when emitting the inline function code repeatedly, as the scope of the logic is isolated to that particular invocation. Were this an actual function then you would have to rely on the JIT to perform this sort of optimization at runtime after warming up.


kotlin  java 
comments powered by Disqus