Adding type instanceof

A bit specific, but it could be great in kotlin :

inline fun <reified T> test() = when(T) {
	is Int -> // do stuff

An equivalent without T :

when("for test") {
	is Int -> // do stuff

Both examples do much different things. The latter checks the runtime type of a provided object. The first uses a compile type and is not related to any objects.

Also note you can do this:

inline fun <reified T> test() = when(T::class) {
    Int::class -> // do stuff

I’ve had cases where I want to switch on T, but switching on T::class introduces inefficient code that could be trivially optimized. Some compiler improvements to support this would actually be very helpful!

1 Like