简体   繁体   中英

Why can't I use a method in a lambda?

Code:

source.subscribe(
                string -> {
                    if( observed ) destination.onNext( reverse( string ) );
                }
        );



    private String reverse( String forward ) {
        return new StringBuilder( forward ).reverse().toString();
    }

source is declared ObservableSource<String> .

destination is Observer<String> .

Android Studio underlines string in the call to reverse and tells me " reverse(java.lang.String) in StringReverser (my enclosing class) cannot be applied to (<lambda parameter>) ".

I don't get it. source is delivering Strings, destination consumes Strings. and the reverse method takes a String parameter. What's the problem?

As @Andreas pointed out in the comments, I was treating an RxJava 2 Observer like an old-style RxJava 1 Subscriber . Doesn't work! Once I implemented all 4 Observer methods ( onSubscribe(), onNext(), onError() and onComplete() ) the error went away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM