[
https://issues.apache.org/jira/browse/GROOVY-11863?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18061697#comment-18061697
]
Eric Milles edited comment on GROOVY-11863 at 2/27/26 5:55 PM:
---------------------------------------------------------------
You should rewrite "MAX_LINES" as "Test.MAX_LINES" or "this.@MAX_LINES" or
"owner.@MAX_LINES". The map delegate steps in front of all property name
resolution from within that closure. Or making the constant public would boost
its precedence. Or you could make the constant a final local variable instead
of a static field.
was (Author: emilles):
You should rewrite "MAX_LINES" as "Test.MAX_LINES" or "owner.@MAX_LINES". The
map delegate steps in front of all property name resolution from within that
closure. Or making the constant public would boost its precedence. Or you
could make the constant a final local variable instead of a static field.
> False type checking error for class with delegate field
> -------------------------------------------------------
>
> Key: GROOVY-11863
> URL: https://issues.apache.org/jira/browse/GROOVY-11863
> Project: Groovy
> Issue Type: Bug
> Components: Static Type Checker
> Affects Versions: 5.0.4
> Reporter: Ben Sherman
> Assignee: Eric Milles
> Priority: Minor
>
> Full context is in this GitHub PR:
> [nextflow-io/nextflow#6220|https://github.com/nextflow-io/nextflow/pull/6220/changes#r2666549692]
> I encountered a false type checking error while trying to upgrade Nextflow
> from Groovy 4 to 5. I have created a minimal example below. The comparison
> "c++ < MAX_LINES" is reported as a type mismatch even though both are integer
> types.
> The factors seem to be {-}(1) an inner class{-}, (2) a delegate field, and
> (3) a while loop. But I'm not sure what the exact cause is.
> -In my case, the inner class is static, so I can work around the error by
> moving the inner class outside.-
> Minimal example:
> {code:java}
> import java.nio.file.Path
> import groovy.transform.CompileStatic
> @CompileStatic
> class Test {
> static private int MAX_LINES = 100
> @Delegate
> private Map<String,Object> delegate = [:]
> private String fetch(Path path) {
> int c=0
> path.withReader { reader ->
> // ERROR: Cannot call
> java.lang.Integer#compareTo(java.lang.Integer) with arguments
> [java.lang.Object]
> while( c++ < MAX_LINES ) {
> }
> }
> }
> }{code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)