how does lambda map to functional interface in different locations ?
posted 1 week ago
If I define a functional interface in my class like
Then inside this class, when I do (int a, int b) -> a +b; it knows it should match "MathOperation" interface since that's the only one that matches. However, if the above interface code is NOT in the same class as (int a, int b) -> a +b;
how does it know which one should be ? For example , if I define some interfaces like
Great question. You don't just define a lambda in isolation. Instead you assign it to a variable or pass it as it to a method as a parameter. This means the compiler has context to figure out which interface you are talking about.
In other words, the compiler uses the context to figure out what it should be.