1

I'm trying to understand how lambda is work. In the process i found this example.

public class Calculator {
   interface IntegerMath {
      int operation(int a, int b);

      default IntegerMath swap() {
         return (a, b) -> operation(b, a);
      }
   }

   private static int apply(int a, int b, IntegerMath op) {
      return op.operation(a, b);
   }

   public static void main(String[] args) {
      IntegerMath addition =  (a, b) -> a + b;
      Math subtraction = (a, b) -> a - b;

      System.out.println(apply(5, 10, subtraction));
      System.out.println(apply(5, 10, subtraction.swap()));
   }
}

I'm trying to convert the swap method implementation from lambda to normal method.

But i cant change that, because if i did that i should write the implementation of operation method and create an infinity recursive.

public class Calculator {
   interface IntegerMath {
      int operation(int a, int b);

      default IntegerMath swap() {
         return new IntegerMath() {
            @Override
            public int operation(int a, int b) {
               return operation(b, a);
            }
         };
      }
   }
}

Can i not use lambda in swap method in interface IntegerMath?

  • 1
    Could you try to rephrase the question please? It's not really clear what you're asking. Your fist code block is not valid Java. Should it be `public interface Calculator`? – mypetlion Jul 29 '19 at 18:30
  • You are returning different things in each implementation. swap() returns a *function* that can be later called. Your implementation of swap() simply won't work because it will infinitely recurse and wants to return a *value* not a function. – Ryan Schaefer Jul 29 '19 at 18:30
  • @mypetlion sorry my bad. i have fixed my question – Naufal Muntaaza Jul 29 '19 at 18:45
  • @RyanSchaefer you are right. can i not use lambda in method swap in IntegerMath interface – Naufal Muntaaza Jul 29 '19 at 18:51
  • Possible duplicate of [Calling outer class function from inner class](https://stackoverflow.com/questions/2808501/calling-outer-class-function-from-inner-class) – shmosel Jul 29 '19 at 19:03

1 Answers1

2

If you're asking how to call the outer operation() from within your anonymous implementation, you can do it like this:

return IntegerMath.this.operation(b, a);
shmosel
  • 49,289
  • 6
  • 73
  • 138
  • sorry, i have fixed my question. i don't think that works? – Naufal Muntaaza Jul 29 '19 at 18:49
  • Why don't you think so? Did you try it? – shmosel Jul 29 '19 at 18:55
  • i'm try it. but i don't exactly understand your answer, maybe i'm wrong. where i can do that? – Naufal Muntaaza Jul 29 '19 at 19:00
  • How can you do what? – shmosel Jul 29 '19 at 19:01
  • sorry i'm not. i just cant explaining my problem clear enough. check my edited question. i want to change the swap method in IntegerMath interface without using lambda. – Naufal Muntaaza Jul 29 '19 at 19:11
  • I understood the question. But I don't understand what problem you're having with the answer. – shmosel Jul 29 '19 at 19:15
  • in example code i can call subtraction.swap() and the output from that two is different, so the swap method is work using lambda. but what if i don't want to use lambda. in confuse how to implement your answer in swap method. because when i use your answer to replace the lambda it doesn't work. or i have to create anonymous class that implement operation and use your answer but it show warning infinite recursion – Naufal Muntaaza Jul 29 '19 at 19:24