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 2025/05/31 14:56:00 UTC

[jira] [Comment Edited] (GROOVY-11685) Static type checking fails with groovy 5.x

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

Eric Milles edited comment on GROOVY-11685 at 5/31/25 2:55 PM:
---------------------------------------------------------------

In [this method|https://github.com/brown-uk/nlp_uk/blob/f30e69fac5ce739d962d8aa24230655a8912640c/src/main/groovy/ua/net/nlp/tools/tag/TagUnknown.groovy#L63], you rely on "def" to do a lot of heavy lifting.  You could move the default value (empty array list) to an else block under the check for non-empty opToTagMap.

Anyways, besides adding some types along the way, I think the main problem is your closure has two return statements.  One returns a new TaggedToken and the other just returns.  Adding null to the return (with or without a typecast) should fix it up.

{code:groovy}
Map<String, Map<WordReading, Integer>> lemmaSuffixStatsF = [:].withDefault { [:].withDefault { 0 } }
def opToTagMap = lemmaSuffixStatsF[last3] // replace "def" with "Map<WordReading, Integer>"
opToTagMap = opToTagMap.findAll { e -> getCoeff(e, token, idx, tokens) > 0 }
opToTagMap = opToTagMap.toSorted { e -> - getCoeff(e, token, idx, tokens) }

List<TaggedToken> retTokens = [] // move this to else block
retTokens = opToTagMap.collect { /*Map.Entry<WordReading, Integer>*/ e ->
  ...
  if (...) return // probably need to add "(TaggedToken) null"
  ...
  new TaggedToken(value: token, lemma: lemma, tags: wr.postag, confidence: q)
}
return retTokens
{code}

Small nitpick: When you want to collect all non-null things aka "x.collect\{...\}.findAll\{ it != null \}", [findResults|https://github.com/apache/groovy/blob/a095d672630ddab9dfc1b66a52313c1d152f28c5/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java#L6346] does that without the extra findAll step (and intermediate collection).


was (Author: emilles):
In [this method|https://github.com/brown-uk/nlp_uk/blob/f30e69fac5ce739d962d8aa24230655a8912640c/src/main/groovy/ua/net/nlp/tools/tag/TagUnknown.groovy#L63], you rely on "def" to do a lot of heavy lifting.  You could move the default value (empty array list) to an else block under the check for non-empty opToTagMap.

Anyways, besides adding some types along the way, I think the main problem is your closure has two return statements.  One returns a new TaggedToken and the other just returns.  Adding null to the return (with or without a typecast) should fix it up.

{code:groovy}
Map<String, Map<WordReading, Integer>> lemmaSuffixStatsF = [:].withDefault { [:].withDefault { 0 } }
def opToTagMap = lemmaSuffixStatsF[last3] // replace "def" with "Map<WordReading, Integer>"
opToTagMap = opToTagMap.findAll { e -> getCoeff(e, token, idx, tokens) > 0 }
opToTagMap = opToTagMap.toSorted { e -> - getCoeff(e, token, idx, tokens) }

List<TaggedToken> retTokens = [] // move this to else block
retTokens = opToTagMap.collect { /*Map.Entry<WordReading, Integer>*/ e ->
  ...
  if (...) return // probably need to add "(TaggedToken) null"
  ...
  new TaggedToken(value: token, lemma: lemma, tags: wr.postag, confidence: q)
}
return retTokens
{code}

Small nitpick: When you want to collect all non-null things aka "x.collect{...}.findAll{ it != null}", [findResults|https://github.com/apache/groovy/blob/a095d672630ddab9dfc1b66a52313c1d152f28c5/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java#L6346] does that without the extra findAll step (and intermediate collection).

> Static type checking fails with groovy 5.x
> ------------------------------------------
>
>                 Key: GROOVY-11685
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11685
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 5.0.0-beta-1
>            Reporter: Andriy Rysin
>            Assignee: Eric Milles
>            Priority: Major
>
> This project can compile and run under groovy-4 but fails static type checking with groovy-5. I could not find what would be wrong with the code.
> [https://github.com/brown-uk/nlp_uk]
> {noformat}
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> file:/home/user/nlp_uk/src/main/groovy/ua/net/nlp/tools/TextUtils.groovy: 182: [Static type checking] - Cannot call java.util.function.Consumer#call(capture-o
> f ? extends ua.net.nlp.tools.TextUtils.ResultBase) with arguments [ua.net.nlp.tools.TextUtils$ResultBase]
>  @ line 182, column 6.
>                                         postProcessClosure(analyzed)
>         ^
> file:/home/user/nlp_uk/src/main/groovy/ua/net/nlp/tools/TextUtils.groovy: 200: [Static type checking] - Cannot call java.util.function.Consumer#call(capture-o
> f ? extends ua.net.nlp.tools.TextUtils.ResultBase) with arguments [ua.net.nlp.tools.TextUtils$ResultBase]
>  @ line 200, column 4.
>                         postProcessClosure(analyzed)
>       ^
> file:/home/user/nlp_uk/src/main/groovy/ua/net/nlp/tools/tag/TagUnknown.groovy: 97: [Static type checking] - Incompatible generic argument types. Cannot assign
>  java.util.List<java.lang.Object> to: java.util.List<ua.net.nlp.tools.tag.TagTextCore.TaggedToken>
>  @ line 97, column 25.
>                retTokens = opToTagMap.collect { e ->
>                            ^
> file:/home/user/nlp_uk/src/main/groovy/ua/net/nlp/tools/tag/TagUnknown.groovy: 136: [Static type checking] - Incompatible generic argument types. Cannot assig
> n java.util.List<? extends java.lang.Object> to: java.util.List<ua.net.nlp.tools.tag.TagTextCore.TaggedToken>
>  @ line 136, column 16.
>            return retTokens
>                   ^
> {noformat}



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