# Adding type instanceof

**URL:** https://discuss.kotlinlang.org/t/adding-type-instanceof/25668
**Category:** Language Design
**Created:** [September 16, 2022, 7:51pm UTC](https://discuss.kotlinlang.org/t/adding-type-instanceof/25668 "2022-09-16T19:51:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Qg9](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/qg9/32/9342_2.png) [@Qg9](https://discuss.kotlinlang.org/u/Qg9)
#### Post date: [September 16, 2022, 7:51pm UTC](https://discuss.kotlinlang.org/t/adding-type-instanceof/25668/1 "2022-09-16T19:51:19Z")

</div>

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

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

```

An equivalent without T :

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

```

---

<div class="post-metadata">

### Author: ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)
#### Post date: [September 16, 2022, 8:07pm UTC](https://discuss.kotlinlang.org/t/adding-type-instanceof/25668/2 "2022-09-16T20:07:29Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![derektom14](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/derektom14/32/7411_2.png) [@derektom14](https://discuss.kotlinlang.org/u/derektom14)
#### Post date: [September 19, 2022, 8:54am UTC](https://discuss.kotlinlang.org/t/adding-type-instanceof/25668/3 "2022-09-19T08:54:29Z")

</div>

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!
