Calling java function from kotlin


#1

Hello,
When using RxJava in Kotlin.When subscribing to a Single I use the following subscribe() overload

public final fun subscribe( onCallback: ((t1: User!, t2: Throwable!) -> Unit)!)

In the implementation I have a following check:

        if (t1 == null) return@subscribe

This check generates a compile time warning that (t1==null) is always false
I wonder how does compiler know that at compile time if User! means that t1 can be of type User or User?

Thanks.
Leszek


#2

It looks like you’re trying to specify platform types for User! and Throwable!, which is not possible. Is that the actual signature? Here’s the docs on platform types and null safety


#3

Thanks.
Actual signature is:

public final Disposable subscribe(final BiConsumer<? super T, ? super Throwable> onCallback)

which in Kotlin is:

public final fun subscribe( onCallback: ((t1: User!, t2: Throwable!) -> Unit)!)


#4

Can you post the code around your if (t1 == null) return@subscribe line? When a platform type is returned, how you declare it tells the compiler if it should be considered nullable or not.


#5

Sure, this is what I have:

.subscribe { source, _ ->
if (source == null) return@subscribe
doSomething(source)
}

Thanks.