0

I'm trying to learn Java Lamdas.

I'm trying to convert the following code into lambda representation but it's not working:

private void foo(Data data) {
    Observable.just(data).subscribeWith(new DisposableObserver<Data>() {
        int count = 0;
        int pageCount = 0;

        @Override
        public void onNext(Data data) {
            Timber.e("onNext()");
            count = data.getCount();
            pageCount = data.getPage();
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onComplete() {
            Log.e("class", "onComplete");
            abc(count, pageCount);
        }
    });
}

private void bar(Data data) {
    Observable.just(data).subscribeWith({
        int count = 0;
        int pageCount = 0;

        data -> {
            Timber.e("onNext()");
            count = data.getCount();
            pageCount = data.getPage();
        },
        e  -> e.printStackTrace(),
        () -> {
            Log.e("class", "onComplete");
            abc(count, pageCount);
        }
    });
}

This is giving me error. I'm not really sure how to fix it.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
user2498079
  • 2,872
  • 8
  • 32
  • 60

1 Answers1

4

You missed one essential part. You can't just turn any anonymous inner class instantiation into a lambda.

Quoting from here:

The major benefit of java 8 functional interfaces is that we can use lambda expressions to instantiate them and avoid using bulky anonymous class implementation.

The core property of a functional interface: it has one single abstract method.

More details can be found here for example.

Beyond that, I think the real answer is on a meta level: you have to understand what you are doing. Meaning: you don't use this or that concept because you heard about it, and someone said: "that is great". You use it because it makes sense in your context.

And of course, you can only decide whether something is useful, "the right thing" ... when you understand that concept. In other words: the real answer is to step back (for now); and study the concept of lambdas first. And to then decide where using it will give benefits to your code base.

Finally: as expressed by user Holger in the comments - there is another no-go in that inner class; as it contains fields; which are even "shared" by some of the methods; another aspect that prevents transforming this class into a single pure lambda.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • How can I refactor the code above to achieve the lambda notation? – user2498079 Apr 25 '17 at 14:03
  • @user2498079 I don't think you can. If `subscribeWith` took three classes as arguments, each providing a single function, *then* you would be able to. – Michael Apr 25 '17 at 14:08
  • @user2498079 What makes you think you can? And see my updates to the answer. – GhostCat Apr 25 '17 at 14:10
  • Not only the number of methods to implement, also the fact that this inner class declares fields makes it ineligible for converting it to a lambda expression. – Holger May 02 '17 at 17:47