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

Misleading compiler error when generic type supplied to function

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      generic

      Description

      FULL PRODUCT VERSION :
      java version "9.0.4"
      Java(TM) SE Runtime Environment (build 9.0.4+11)
      Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.16299.248]

      A DESCRIPTION OF THE PROBLEM :
      If a user mistakely invokes CompletionStage.thenApply() with a Supplier instead of with a Function then the compiler gives a misleading error message.

      Given:

      CompletionStage.thenApply(() -> methodThatReturnsCompletionStage())

      I get:

      method thenApply in interface java.util.concurrent.CompletionStage<T> cannot be applied to given types;
      required: java.util.function.Function<? super java.util.Map<com.holdmyspot.pyramid.exchange.Currency,java.math.BigDecimal>,? extends U>
      found: ()->string[...]coin)
      reason: cannot infer type-variable(s) U
      (argument mismatch; incompatible parameter types in lambda expression)

      The problem has nothing to do with type-parameter U, but rather it is type-parameter T that has to be changed. Instead of:

        CompletionStage.thenApply(() -> methodThatReturnsCompletionStage())
        
      the code should read:

        CompletionStage.thenApply(ignored -> methodThatReturnsCompletionStage())
        
      Please improve this error message.


      REPRODUCIBILITY :
      This bug can be reproduced always.

        Attachments

          Activity

            People

            • Assignee:
              vromero Vicente Arturo Romero Zaldivar
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: