I am using lambda to implement a functional interface in a Java program below. When a lambda is passed as an argument to a generic method, the compiler puts a "incompatible type" error because it indicates that the lambda implements a Func <Shape> interface in which the compiler interprets the lambda parameter (the "thing") as a form type when the lambda tries to pass its method (testRound), which requires an argument of type Round. This error makes sense to me.
But a reference to an equivalent method does not cause an error message. I was misunderstood that the lambda and method reference, which could replace this lambda, were interchangeable. Now, this is not so.
public class Main { public static void main(String... args) { methodB(thing -> Main.testRound(thing));
Why does the method link succeed when the lambda is not working?
UPDATE
Vince Amy found the answer, which I designated as accepted below. Although this is not part of my question, here are four ways to get around the fact that lambda is only inferred as the
Func<Shape> , if you are really stuck with using lambda:
I see no reason to prefer one of them over the method link, unless I feel that lambdas are a little less dense (and maybe a little more readable). But they are there if you want them.
source share