Derek Peirce
04/27/2025, 6:02 AMCompletable.doOnEvent
, which takes a lambda `(Throwable?) -> Unit`:
myCompletable.doOnEvent { doSomething() }
However, this started throwing exceptions. As it is a Java method, Kotlin automatically regarded it
as Throwable!
and therefore Throwable
, and as soon as the lambda was called with a null value, it did a null check and threw a NPE, even though it was verifying a value that went completely unused.
As the only reason Kotlin automatically treats Throwable!
as Throwable
here is for convenience, could it recognize in this case that the parameter is entirely unused, and therefore not overeagerly perform a null check? The solution to avoid the exception is to specify { _: Throwable? -> doSomething() }
, which I'd rather not need to specify, and I'd especially have preferred not to have to deal with the exception in the first place at all.Klitos Kyriacou
04/28/2025, 9:31 AMdoOnEvent
method is defined to take an argument of @NonNull Consumer<? super Throwable> onEvent
. Therefore the error is in the caller of `doOnEvent`: it should not pass null to it in the first place. Kotlin rightly treats the lambda as taking a Throwable
(not Throwable?
) since it's annotated as @NonNull
. The platform types (such as Throwable!
) are only used when there is no annotation and Kotlin then can't determine the nullability of the type.JP Sugarbroad
04/28/2025, 9:54 PMDerek Peirce
04/29/2025, 1:21 AMThrowable!
, not Throwable
, because Rx had no nullability parameter on the ? super Throwable
.Klitos Kyriacou
04/29/2025, 8:27 AMThrowable!
it should allow it to be null (after all, that's what platform types are all about).Derek Peirce
06/13/2025, 5:47 AM