Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8260892

Compilation fails: lambda parameter not visible in body when generics involved

    XMLWordPrintable

Details

    • b09
    • x86_64
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10

      A DESCRIPTION OF THE PROBLEM :
      A very specific combination of a lambda and a generic method confuses type inference, such that the compiler doesn't see the lambda parameter. Same compilation occurs on version 17-ea.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the source

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Successful compilation
      ACTUAL -
      example\Example.java:19: error: cannot find symbol
                      return func2.apply(func1.apply(arg));
                                                     ^
        symbol: variable arg
      1 error


      ---------- BEGIN SOURCE ----------
      package example;

      import java.util.function.Function;
      import java.util.function.IntFunction;

      class Example {
          interface GenericOp<A> {
              <B> A apply(IntFunction<B> func1, Function<B, A> func2);
          }

          static <A> GenericOp<A> foo(IntFunction<GenericOp<A>> f) {
              throw new UnsupportedOperationException();
          }

          static <A> GenericOp<A> bar() {
              return foo((int arg) -> new GenericOp<>() {
                  @Override
                  public <B> A apply(IntFunction<B> func1, Function<B, A> func2) {
                      return func2.apply(func1.apply(arg));
                  }
              });
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Adding an explicit type variable in the construction of the anonymous class gives the compiler enough information:

              return foo((int arg) -> new GenericOp<A>() {


      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: