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

[STC] Cannot detect the type of Optional in some cases

    XMLWordPrintableJSON

Details

    Description

      While working with Micronaut, I encountered a problem related to Groovy STC. I think the following code should compile, but it fails at line 13 with 

      [Static type checking] - Cannot find matching method B#intValue(). Please check if the declared type is correct and if the method exists.
       at line: 13, column: 27

      There is a simple workaround, as demonstrated in printOptionalSecondTry(), that compiles correctly.

      @Grab(group='io.micronaut', module='micronaut-http', version='4.2.2')
      import groovy.transform.CompileStatic
      import io.micronaut.http.HttpResponseFactory
      import io.micronaut.http.HttpResponse
      
      @CompileStatic
      class MyClass {
          static <T> HttpResponse<T> fetchMeHttpResponse(T value) {
              return HttpResponseFactory.INSTANCE.ok(value) as HttpResponse<T>
          }
      
          static printOptional() {
              Integer myValue = fetchMeHttpResponse(10G).body.orElseThrow(() -> new RuntimeException("blah blah")).intValue()
              println myValue
          }
          
          static printOptionalSecondTry() {
              BigInteger myOptionalValue = fetchMeHttpResponse(10G).body.orElseThrow(() -> new RuntimeException("blah blah"))
              Integer myValue = myOptionalValue.intValue()
              println myValue
          }
      }
      
      MyClass.printOptional()
      MyClass.printOptionalSecondTry()
      

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              dmurat Damir Murat
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: