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
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 ofAnotherType
.
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.)
Ah okay. Thanks!