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

STC can't distinguish SAMs based on arity

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.7
    • 4.0.0-alpha-3
    • Static Type Checker
    • None

    Description

      I'm using Vavr, which has a number of methods with overloads that take alternately a Supplier or a Function (particularly for creating an exception instance). The STC can't disambiguate which overload to call, even when the syntax is explicit.

      In the below code, each call to replace is entirely unambiguous, but the compiler reports "Reference to method is ambiguous" on both. Of course, the problem persists with closures. Only wrapping the entire lambda (including ()) in parens and using as Function (or Supplier) works.

      @CompileStatic
      class Groovy9881 {
      
          def doReplace() {
              new Value(123)
                  .replace(() -> "foo")
      
              new Value(123)
                  .replace((Integer v) -> "bar")
          }
      
          static class Value<V> {
              final V val
      
              Value(V v) {
                  this.val = v
              }
      
              <T> Value<T> replace(Supplier<T> supplier) {
                  new Value<>(supplier.get())
              }
      
              <T> Value<T> replace(Function<? super V, ? extends T> function) {
                  new Value(function.apply(val))
              }
          }
      }
      
      

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            emilles Eric Milles
            chrylis Christopher Smith
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 50m
                50m

                Slack

                  Issue deployment