It seems like it should be allowed, yet this fails...
# getting-started
c
It seems like it should be allowed, yet this fails to compile:
Copy code
interface 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)
s
out
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.
So it’s not possible to call a method where one of the input types is
out
projected
r
You need to make the signature for
create
match the signature for A:
Copy code
fun <P:  Param?> create
🙏 1
j
So that the type param for
A
will get passed on, and you end up with
a
being of type
A<Nothing?>
as you expect, instead of type
A<out Any?>
.
r
Assuming
Param
is doing something useful this probably isn't what you want anyway because you can't call v(Param) on A<Nothing?> 🤔
Copy code
println(a.v(null as Param?))
    println(a.v(object: Param {}))
^^^compile errors^^^
c
I see, thanks.