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

Closure to SAM coercion doesn't handle overloads with different SAM signatures

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.4
    • 4.0.0-alpha-3
    • None
    • None

    Description

      The following…

      interface Action<T> {
        void exec(T thing)
      }
      
      interface Block {
        void exec()
      }
      
      class Container {
        void m(Action<?> action) {}
        void m(Block block) {} 
      }
      
      def c = new Container()
      
      c.m { -> 1 }
      

      Yields…

      groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method Container#m.
      Cannot resolve which method to invoke for [class ConsoleScript0$_run_closure1] due to overlapping prototypes between:
      	[interface Action]
      	[interface Block]
      	at ConsoleScript0.run(ConsoleScript0:16)
      

      It seems that Groovy should be able to deal with the case where the SAM parameter signatures are different. Java does this with lambda expressions. That is, the following works in Java…

      Container c = new Container();
      c.m(() -> {});
      c.m(arg -> {});
      

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              ldaley Luke Daley
              Votes:
              2 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: