Lambda return types and overloaded methods

Greenaway, Chris Chris.Greenaway at softwareag.com
Tue Aug 16 09:21:42 UTC 2016


There seems to be a problem with matching the return type of a lambda to the type required by an overloaded method when the lambda parameter isn't explicitly typed.

For example, the code below has compilation failures on the lines that are marked with "// Error". All other lines are acceptable.

There also seems to be an issue with method references when there are multiple methods with the same name but different arity - see the line marked "// Error 8".

            Chris Greenaway.


import java.util.function.Function;
import java.util.function.Predicate;

public class LambdaOverloading {
    public static void main(String[] args) {
        LambdaPredicate predicate = new LambdaPredicate();
        predicate.run((String s) -> s.startsWith("X"));
        predicate.run((String s) -> s.isEmpty());
        predicate.run((String s) -> true);
        predicate.run(s -> s.startsWith("X"));
        predicate.run(s -> s.isEmpty());
        predicate.run(s -> true);
        predicate.run(String::isEmpty);

        LambdaFunction function = new LambdaFunction();
        function.run((String s) -> s.substring(1));
        function.run((String s) -> s.trim());
        function.run((String s) -> s.toUpperCase());
        function.run((String s) -> "");
        function.run(s -> s.substring(1));
        function.run(s -> s.trim());
        function.run(s -> s.toUpperCase());
        function.run(s -> "");
        function.run(String::trim);
        function.run(String::toUpperCase);

        LambdaOverloaded overloaded = new LambdaOverloaded();
        overloaded.run((String s) -> s.startsWith("X"));
        overloaded.run((String s) -> s.substring(1));
        overloaded.run((String s) -> s.isEmpty());
        overloaded.run((String s) -> s.trim());
        overloaded.run((String s) -> s.toUpperCase());
        overloaded.run((String s) -> true);
        overloaded.run((String s) -> "");
        overloaded.run(s -> s.startsWith("X"));                     // Error 1
        overloaded.run(s -> s.substring(1));                        // Error 2
        overloaded.run(s -> s.isEmpty());                           // Error 3
        overloaded.run(s -> s.trim());                              // Error 4
        overloaded.run(s -> s.toUpperCase());                       // Error 5
        overloaded.run(s -> true);                                  // Error 6
        overloaded.run(s -> "");                                    // Error 7
        overloaded.run(String::isEmpty);
        overloaded.run(String::trim);
        overloaded.run(String::toUpperCase);                        // Error 8
    }

    private static class LambdaPredicate {
        public boolean run(Predicate<String> predicate) {
            return predicate.test("test");
        }
    }

    private static class LambdaFunction {
        public String run(Function<String, String> function) {
            return function.apply("test");
        }
    }

    private static class LambdaOverloaded {
        public boolean run(Predicate<String> predicate) {
            return predicate.test("test");
        }

        public String run(Function<String, String> function) {
            return function.apply("test");
        }
    }
}
This communication contains information which is confidential and may also be privileged. It is for the exclusive use of the intended recipient(s). If you are not the intended recipient(s), please note that any distribution, copying, or use of this communication or the information in it, is strictly prohibited. If you have received this communication in error please notify us by e-mail and then delete the e-mail and any copies of it.
Software AG (UK) Limited Registered in England & Wales 1310740 - http://www.softwareag.com/uk

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20160816/70b305b0/attachment-0001.html>


More information about the compiler-dev mailing list