In Kotlin. I need a lambda: `(T)->Unit` `<T:...
# getting-started
i
In Kotlin. I need a lambda:
(T)->Unit
`<T: Any`> but allow null when T is nullable type... How to do that? Thank you
s
<T : Any>
disallows
T
from being a nullable type
maybe you can accept
T?
instead?
f
<T : Any?>
s
you can just get rid of the type bound since
T
is allowed to be
Any?
by default lol
I’m assuming you can’t just change the bound, otherwise you wouldn’t have this problem
c
If you do
<T>
, it should be able to accept both nullable and non-null types. The type of
(T)->Unit
should then be the same. https://pl.kotl.in/Zo8dB908p
d
They are suggesting to remove the
: Any
type bound. This bound prevents T from being nullable, because
null
is not assignable to
Any
The default type bound, if you specify none, is
Any?
, which
null
is assignable to.
i
Thank you for your helps.
I can't change the bound because I need KClass<T> parameter...