genericskotlinreification

Filter for generic type without reflection or casting


In Kotlin there is a limited form of reified generics. Is there any way to use reification to filter for a generic type without using getClass() or as or any kind of weird annotation, ie. just by using the is keyword? For example, I have the following structure:

import java.util.*

internal class Layout<out T : LayoutProtocol>(val t: T) {
    fun getName(): String {
        return t.getName()
    }
}

interface LayoutProtocol {
    fun getName(): String
}

internal class Vertical : LayoutProtocol {
    override fun getName(): String {
        return "Vertical"
    }
}

internal class Horizontal : LayoutProtocol {
    override fun getName(): String {
        return "Horizontal"
    }
}

fun main(args: Array<String>) {
    val layouts = LinkedList<Layout<*>>()
    layouts.add(Layout<Horizontal>(Horizontal()))
    layouts.add(Layout<Vertical>(Vertical()))
    println("Horizontal layouts:")
    layouts.filterIsInstance<Layout<Horizontal>>().forEach { println(it.getName()) }
}

This outputs:

Horizontal layouts:
Horizontal
Vertical

I would like it to output the following. Is there any way to get:

Horizontal layouts:
Horizontal

If we look at the source code for filterIsInstance(...), Kotlin does some tricky stuff to circumvent type erasure, but still does not work:

/**
 * Returns a list containing all elements that are instances of specified type parameter R.
 */
public inline fun <reified R> Iterable<*>.filterIsInstance(): List<@kotlin.internal.NoInfer R> {
    return filterIsInstanceTo(ArrayList<R>())
}

/**
 * Appends all elements that are instances of specified type parameter R to the given [destination].
 */
public inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterIsInstanceTo(destination: C): C {
    for (element in this) if (element is R) destination.add(element)
    return destination
}

If this is not possible in Kotlin, is there any language (JVM or non-JVM) that lets me do something like the following:

inline fun <reified R: LayoutProtocol> filterVerticals(from: Iterable<Layout<R>>): Iterable<Layout<Vertical>> {
    val dest = ArrayList<Layout<Vertical>>()

    for (element in from)
        if (element is Layout<Vertical>)
            dest.add(element)

    return dest
}

Solution

  • There is no simple way to do this because of type erasure, but if you really want and performance/readability/error-proneness is not something you are worried about, you can do a few tricks:

    First, let's add a factory method to Layout to preserve erased type

    open internal class Layout<T : LayoutProtocol>(val t: T) {
      ...
      companion object {
        inline fun <reified T: LayoutProtocol> create(instance: T): Layout<T> {
          return object: Layout<T>(instance) {}
        }
      }
    }
    

    (note: here I removed out variance for simplicity)

    Second, you need a helper class

    open class TypeLiteral<T> {
      val type: Type = getSuperclassTypeParameter(javaClass)
    
      companion object {
        fun getSuperclassTypeParameter(subclass: Class<*>) =
          (subclass.genericSuperclass as ParameterizedType).actualTypeArguments[0]
      }
    }
    

    (note: the same approach is used by Guice DI, it contains a production-ready TypeLiteral implementation )

    And, finally our own filter method

    inline fun <reified R> Iterable<*>.genericFilterIsInstance() where R : Any =
      filterIsInstance<R>()
      .filter { object : TypeLiteral<R>() {}.type == it.javaClass.genericSuperclass }
    

    And now it prints exactly what you want

    fun main(args: Array<String>) {
      val layouts = LinkedList<Layout<*>>()
      layouts.add(Layout.create(Horizontal()))
      layouts.add(Layout.create(Vertical()))
      println("Horizontal layouts:")
      layouts.genericFilterIsInstance<Layout<Horizontal>>().forEach { println(it.getName()) }
      /* prints:
      Horizontal layouts:
      Horizontal
       */
    }
    

    But please, don't use this answer in production code. In real life passing a class instance for filtering would always be preferable.