I want to know whether it's possible to force kotlin to infer the non-nullable of a given type inference. Consider the following example:
abstract class Wrapper<T>
class StringWrapper : Wrapper<String>()
fun <O, P> wrap(property: KProperty1<O, P>, wrapper: Wrapper<P>) {
}
When I call wrap
on a non-nullable property, everything works fine:
data class NonNullableExample(val value: String)
wrap(NonNullableExample::value, StringWrapper())
But when I call wrap
on a nullable property, I get a compiler error, because the inference of P
is nullable, whereas StringWrapper
isn't:
data class NullableExample(val value: String?)
wrap(NullableExample::value, StringWrapper())
Type inference failed: Cannot infer type parameter
P
in
fun <O, P> wrap(property: KProperty1<O, P>, wrapper: Wrapper<P>): Unit
None of the following substitutions
(KProperty1<NullableExample, String>, Wrapper<String>)
(KProperty1<NullableExample, String?>, Wrapper<String?>)
can be applied to
(KProperty1<NullableExample, String?>, StringWrapper)
So basically what I want is, regardless of P
being nullable or non-nullable, P
of Wrapper<P>
should always be the non-nullable form of P
. Is this possible?
value
but is non-nullable, and doNullableExample::NonNullableValue
when callingwrap
? Of course you'll need to deal with what happens whenvalue
isnull
, but you'll need to deal with that somewhere anyway, right? – jingx