[
https://issues.apache.org/jira/browse/GROOVY-9595?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Abhiyank updated GROOVY-9595:
-----------------------------
Description:
i think we need to re-open this ticket, the way final keyword behaves is not
right.
A final object is not Immutable, you can call accessor methods on it, which is
broken now:
Consider following code:
{noformat}
class TestGroovy {
public static void main(String[] args) {
final Set<String> testSet = []
for (int i = 0; i < 4; i++) {
testSet += "asd" + i
}
println testSet
}
}
{noformat}
Which results in following output:
{noformat}
PS E:\Projects> groovy -v
Groovy Version: 3.0.4 JVM: 1.8.0_221 Vendor: Oracle Corporation OS: Windows
Server 2019
PS E:\Projects> groovy .\TestGroovy.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
E:\Projects\TestGroovy.groovy: 6: The variable [testSet] is declared final but
is reassigned
. At [6:13] @ line 6, column 13.
testSet += "asd" + i
^
1 error
{noformat}
So if the Set object is not Immutable why can't it be modified? Similar results
are observed when you try to set a final object's field.
We should be following Java's definition of final. I think this article covers
the final behaviour in detail: https://www.baeldung.com/java-final
was:
i think we need to re-open this ticket, the way final keyword behaves is not
right.
A final object is not Immutable, you can call accessor methods on it, which is
broken now:
Consider following code:
class TestGroovy {
public static void main(String[] args) {
final Set<String> testSet = []
for (int i = 0; i < 4; i++) {
testSet += "asd" + i
}
println testSet
}
}
Which results in following output:
PS E:\Projects> groovy -v
Groovy Version: 3.0.4 JVM: 1.8.0_221 Vendor: Oracle Corporation OS: Windows
Server 2019
PS E:\Projects> groovy .\TestGroovy.groovy
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
E:\Projects\TestGroovy.groovy: 6: The variable [testSet] is declared final but
is reassigned
. At [6:13] @ line 6, column 13.
testSet += "asd" + i
^
1 error
So if the Set object is not Immutable why can't it be modified? Similar results
are observed when you try to set a final object's field.
We should be following Java's definition of final. I think this article covers
the final behaviour in detail: https://www.baeldung.com/java-final
> Groovy final keyword behaves more like Immutable, doesn't follow java's final
> keyword behaviour
> -----------------------------------------------------------------------------------------------
>
> Key: GROOVY-9595
> URL: https://issues.apache.org/jira/browse/GROOVY-9595
> Project: Groovy
> Issue Type: Bug
> Components: Compiler
> Affects Versions: 2.5.6, 3.0.4
> Reporter: Abhiyank
> Priority: Major
>
> i think we need to re-open this ticket, the way final keyword behaves is not
> right.
> A final object is not Immutable, you can call accessor methods on it, which
> is broken now:
> Consider following code:
> {noformat}
> class TestGroovy {
> public static void main(String[] args) {
> final Set<String> testSet = []
> for (int i = 0; i < 4; i++) {
> testSet += "asd" + i
> }
> println testSet
> }
> }
> {noformat}
> Which results in following output:
> {noformat}
> PS E:\Projects> groovy -v
> Groovy Version: 3.0.4 JVM: 1.8.0_221 Vendor: Oracle Corporation OS: Windows
> Server 2019
> PS E:\Projects> groovy .\TestGroovy.groovy
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
> failed:
> E:\Projects\TestGroovy.groovy: 6: The variable [testSet] is declared final
> but is reassigned
> . At [6:13] @ line 6, column 13.
> testSet += "asd" + i
> ^
> 1 error
> {noformat}
> So if the Set object is not Immutable why can't it be modified? Similar
> results are observed when you try to set a final object's field.
> We should be following Java's definition of final. I think this article
> covers the final behaviour in detail: https://www.baeldung.com/java-final
--
This message was sent by Atlassian Jira
(v8.3.4#803005)