Why the Lambda formula can be passed to the RxJava method compose argument

Asked 1 years ago, Updated 1 years ago, 91 views

For example, the method compose of Observable is defined as follows:
I think the argument should be given a variable of type ObservableTransformer, but why can I give it a function of type Lambda?

@SuppressWarnings("unchecked")
@CheckReturnValue
@SchedulerSupport.NONE
public final<R>Observable<R>compose(ObservableTransformer<?super T,?extends R>composer){
    return wrap((ObservableTransformer<T,R>)ObjectHelper.requireNonNull(composer, "composer is null")).apply(this));
}

java reactivex

2022-09-29 20:28

1 Answers

In order to be able to use lambda, it is only necessary to have an interface with only one method.That's the only functional interface requirement.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.8

A functional interface is an interface that has just one abstract method

http://reactivex.io/RxJava/javadoc/io/reactivex/ObservableTransformer.html

ObservableTransformer satisfies that.

@FunctionalInterface Annotation is for warning you when you accidentally write do not meet the functional interface requirements (similar to @Override).

https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.6.4.9


2022-09-29 20:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.