You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/12/24 11:21:00 UTC

[jira] [Commented] (GROOVY-11257) [STC] Cannot detect the type of Optional in some cases

    [ https://issues.apache.org/jira/browse/GROOVY-11257?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17800164#comment-17800164 ] 

Eric Milles commented on GROOVY-11257:
--------------------------------------

You can also use {{fetchMeHttpResponse(...).getBody().orElseThrow(...)}} as a workaround.  The bug exists in the property handling.

> [STC] Cannot detect the type of Optional in some cases
> ------------------------------------------------------
>
>                 Key: GROOVY-11257
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11257
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 4.0.17
>            Reporter: Damir Murat
>            Assignee: Eric Milles
>            Priority: Major
>             Fix For: 5.0.0-alpha-5
>
>
> 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 
> {code:java}
> [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{code}
> There is a simple workaround, as demonstrated in printOptionalSecondTry(), that compiles correctly.
> {code:java}
> @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()
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)