DEV Community

TheCodeAlchemist
TheCodeAlchemist

Posted on

When Lambdas get confused

Lambdas are smart — they can infer a lot of details from the context.

But, sometimes, they can’t. In such cases, a lambda needs more information from the programmer.

Here’s one such case

Interested in details? Please watch this video — https://youtu.be/JQX9JIKF5CQ

Consider we have two functional interfaces —

SumFunctionInterface → To perform the sum

MultiplyFunctionalInterface → To perform the multiplication

public interface SumFunctionalInterface {
int sum(int a, int b);
}
public interface MultiplyFunctionalInterface {
int multiply(int a, int b);
}

Then, we have the Runner class which has two overloaded methods with name m1 — one accepts SumFunctionalInterface and another MultiplyFunctionalInterface

public class Runner {

    public static void main(String[] args) {
        int sumResult = m1((x, y) -> x + y, 5, 6);
    }

    //Method 1
    private static int m1(SumFunctionalInterface iface, int x, int y) {
        return iface.sum(x, y);
    }

    //Method 2
    private static int m1(MultiplyFunctionalInterface iface, int x, int y) {
        return iface.multiply(x, y);
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, if we try to call m1 and pass it a lambda to do the summation(see above) then we’ll a compile time error because this time compiler is not able to figure out which version of m1 needs to be called here.

There’s a conflict — both functional interfaces have the same method signature and passed lambda is a match for both of them so it’s getting confused.

To fix that, we will have to provide additional information for the compiler. We can add an explicit cast which will resolve this ambiguity.

public static void main(String[] args) {
    int sumResult = m1((SumFunctionalInterface) (x, y) -> x + y, 5, 6);
}
Enter fullscreen mode Exit fullscreen mode

Want to learn more about Lambdas and Streams? Please visit my playlist https://www.youtube.com/playlist?list=PLpxcSt9FGVVGl_IwSP1o4AmSrQc6z4SFt

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay