CLOVIS
09/26/2022, 9:18 AMinterface Param
data class A<P : Param?>(val v: (P) -> Unit)
fun <P> create(v: (P) -> Unit) = A(v)
fun main() {
val a = create { _: Nothing? -> }
println(a.v(null)) // Null can not be a value of a non-null type Nothing
}
According to IDEA, a
has type A<out Any?>
, so why is null
not allowed? (playground link)Sam
09/26/2022, 9:35 AMout
means that the type can only be used as a return/output type; in
means that the type can only be used as a parameter/input type.Sam
09/26/2022, 9:38 AMout
projectedRobert Williams
09/26/2022, 9:41 AMcreate
match the signature for A:
fun <P: Param?> create
Juliane Lehmann
09/26/2022, 9:45 AMA
will get passed on, and you end up with a
being of type A<Nothing?>
as you expect, instead of type A<out Any?>
.Robert Williams
09/26/2022, 10:17 AMParam
is doing something useful this probably isn't what you want anyway because you can't call v(Param) on A<Nothing?> 🤔Robert Williams
09/26/2022, 10:19 AMprintln(a.v(null as Param?))
println(a.v(object: Param {}))
^^^compile errors^^^CLOVIS
09/26/2022, 10:29 AM