Considering : ```inline fun <T> List<T&gt...
# announcements
l
Considering :
Copy code
inline fun <T> List<T>.applyOnEach(block: T.() -> Unit): List<T> = onEach { it?.block() }
Is there a way to mark the receiver in
T.() -> Unit
as not nullable?
j
Bound the generic
<T : Any>
3
l
But I want the elements to be nullable. Though I am not calling the block if the element is nullable. Actually, when call
applyOnEach
, the inferred type is nullable
z
Copy code
fun <T : Any> List<T?>.applyOnEach(block: T.() -> Unit)
l
I did try something like that. Problem is that the return type must become nullable as well. So if I call the method with a list of not nullable, it returns a list of nullable regardless
z
Yea, unfortunately i don’t think there’s a way to express that
m
A little hacky, but it works:
Copy code
@JvmName("applyOnEachNullable")
fun <T : Any> List<T?>.applyOnEach(block: T.() -> Unit): List<T?> = onEach { it?.block() }
fun <T : Any> List<T>.applyOnEach(block: T.() -> Unit): List<T> = onEach { it.block() }
z
Neat!
l
That is what I ended up doing. Not exactly satisfied but I also think it’s the best I can do for now. Thank you