passing a observer and passing a lambda to a skipUntil method

Viewed 144
RxJava 2

I have the following 2 examples of how a skipUntil is used:

This one passes a lambda function to the skipUntil:

   sourceSubject
            .skipUntil<Int> {
                triggerSubject.subscribe(it)
            }
            .subscribeBy { println(it) }

And this one passes the observer:

sourceSubject
        .skipUntil(triggerSubject)
        .subscribeBy { println(it) }

However, I am confused with the method signature as it only seems to take a Observer and I couldn't see the lambda. Just wondering why I can pass a observer and pass a lambda to this function

Here is the method that just seems to show passing in an Observer:

public final <U> Observable<T> skipUntil(ObservableSource<U> other) {
        ObjectHelper.requireNonNull(other, "other is null");
        return RxJavaPlugins.onAssembly(new ObservableSkipUntil<T, U>(this, other));
    }

Many thanks in advance

1 Answers

This feature is knows as the SAM Conversions. it basically means

if an object implements a SAM interface (one with a Single Abstract Method), you can pass a lambda instead of creating and passing an object of the given interface.

In your case skipUntil expects a parameter of type ObservableSource and since ObservableSource is SAM interface, you can pass a lambda instead of a full fledged object of type ObservableSource.

So even though there is no skipUntil function which expects a lambda, you can pass one because of SAM Conversions.

Related