[英]Lambda function without parameters - Cannot infer functional interface type
I have a lambda function that doesn't require any parameter:我有一个不需要任何参数的 lambda function:
Observable.interval(5, TimeUnit.SECONDS)
.subscribe(x -> new CryptoCompareRxService().getHistoricalDaily("BTC", "USD")
.subscribe(historicalDaily -> System.out.println("historicalDaily = " + historicalDaily)));
I would like to replace the " x
" by " ()
" because it is useless:我想用“ ()
”替换“ x
”,因为它没用:
Observable.interval(5, TimeUnit.SECONDS)
.subscribe(() -> new CryptoCompareRxService().getHistoricalDaily("BTC", "USD")
.subscribe(historicalDaily -> System.out.println("historicalDaily = " + historicalDaily)));
But when I do that, I get an error:但是当我这样做时,我得到一个错误:
Cannot infer functional interface type无法推断功能接口类型
Why?为什么?
Here is the full class:这是完整的 class:
package com.tests.retrofitrxjava;
import com.tests.retrofitrxjava.rx.CryptoCompareRxService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import rx.Observable;
import java.util.concurrent.TimeUnit;
@SpringBootApplication
public class RetrofitRxjavaApplication {
public static void main(String[] args) {
SpringApplication.run(RetrofitRxjavaApplication.class, args);
Observable.interval(5, TimeUnit.SECONDS)
.subscribe(x -> new CryptoCompareRxService().getHistoricalDaily("BTC", "USD")
.subscribe(historicalDaily -> System.out.println("historicalDaily = " + historicalDaily)));
}
}
I think your mistake is due to change in RXObservable in version 2, where subscribe accept now Consumer which accept parameter我认为您的错误是由于版本 2 中 RXObservable的更改,其中订阅现在接受接受参数的消费者
public final Disposable subscribe(Consumer<? super T> onNext)
Consumer<T> A functional interface (callback) that accepts a single value. Consumer<T>接受单个值的功能接口(回调)。
and not Action as in version 1而不是版本 1 中的Action
public final Subscription subscribe(Action1<? super T> onNext)
Action1<T> extends Action A one-argument action. Action1<T> extends Action 单参数动作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.