Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Not A Bug
-
2.5.6, 3.0.4
-
None
-
None
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:
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
Attachments
Issue Links
- is caused by
-
GROOVY-1628 Inconsistent checking of final
-
- Closed
-