# Getting the KType of a nullable type

**URL:** <https://discuss.kotlinlang.org/t/getting-the-ktype-of-a-nullable-type/2220>\
**Category:** Support\
**Created:** [January 17, 2017, 3:38am UTC](https://discuss.kotlinlang.org/t/getting-the-ktype-of-a-nullable-type/2220 "2017-01-17T03:38:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![austinv11](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@austinv11](https://discuss.kotlinlang.org/u/austinv11)\
**Post date:** [January 17, 2017, 3:38am UTC](https://discuss.kotlinlang.org/t/getting-the-ktype-of-a-nullable-type/2220/1 "2017-01-17T03:38:34Z")

</div>

I cannot seem to find a way to get the `KType` of a nullable type. I need this because I have a function which analyzes some other function’s value parameter types and uses that to determine how to parse a string in order to be able to invoke that function via reflection using that string for arguments. Additionally it appears that `nullableKType.isSubtypeOf(AnotherType::class.starProjectedType)` returns false even if the nullable KType is a sub type of `AnotherType`. Since this is kinda hard to describe, here is some relevant code where this needs to be applied: [https://github.com/austinv11/KotBot-IV/blob/master/Core/src/main/kotlin/com/austinv11/kotbot/core/api/commands/CommandRegistry.kt#L155](https://github.com/austinv11/KotBot-IV/blob/master/Core/src/main/kotlin/com/austinv11/kotbot/core/api/commands/CommandRegistry.kt#L155)

---

<div class="post-metadata">

**Author:** ![udalov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/udalov/32/5679_2.png) [@udalov](https://discuss.kotlinlang.org/u/udalov)\
**Post date:** [January 17, 2017, 7:48am UTC](https://discuss.kotlinlang.org/t/getting-the-ktype-of-a-nullable-type/2220/2 "2017-01-17T07:48:00Z")

</div>

You can use the `withNullability` extension to alter the nullability of a type:

```
// Corresponds to the type "AnotherType?"
AnotherType::class.starProjectedType.withNullability(true)

```

> Additionally it appears that `nullableKType.isSubtypeOf(AnotherType::class.starProjectedType)` returns false even if the nullable KType is a sub type of `AnotherType`.

This is by design. From Kotlin’s point of view, there can’t be a nullable type which is a subtype of some non-null type. (To understand why this is the case, try viewing a type as a set of values and the subtyping relation as a subset operation.)

---

<div class="post-metadata">

**Author:** ![austinv11](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@austinv11](https://discuss.kotlinlang.org/u/austinv11)\
**Post date:** [January 17, 2017, 12:10pm UTC](https://discuss.kotlinlang.org/t/getting-the-ktype-of-a-nullable-type/2220/3 "2017-01-17T12:10:08Z")

</div>

Ah okay. Thanks!
