Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6488

Closure to SAM coercion cannot infer generic return type

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.2.1
    • Fix Version/s: 2.2.2
    • Component/s: Static compilation
    • Labels:
      None

      Description

      Given the following:

      import groovy.transform.CompileStatic
      
      import java.util.concurrent.Callable
      
      interface CustomCallable<T> {
        T call()
      }
      
      class Thing {
        static <T> T customType(CustomCallable<T> callable) {
          callable.call()
        }
      
        static <T> T callable(Callable<T> callable) {
          callable.call()
        }
      
        @CompileStatic
        static void run() {
          callable { [] }.add(1) // return type is inferred - passes compile
          customType { [] }.add(1) // return type is not inferred - fails compile
        }
      }
      

      The problem is visible in the run() method above.

      It would be extremely useful for typed DSLs if the return type can be inferred for SAM coercion.

        Attachments

          Activity

            People

            • Assignee:
              melix C├ędric Champeau
              Reporter:
              ldaley Luke Daley
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: