Lambda function without parameters - Cannot infer functional interface type

Viewed 541

I have a lambda function that doesn't require any parameter:

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:

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:

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)));

  }

}
1 Answers

I think your mistake is due to change in RX Observable in version 2, where subscribe accept now Consumer which accept parameter

public final Disposable subscribe(Consumer<? super T> onNext)

Consumer<T> A functional interface (callback) that accepts a single value.

and not Action as in version 1

public final Subscription subscribe(Action1<? super T> onNext)

Action1<T> extends Action A one-argument action.

Related