ajwang / groovypptest

Automatically exported from code.google.com/p/groovypptest
0 stars 0 forks source link

Cannot redefine variable inside loop #364

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This is the code:

def s = "foo"
["bar"].each{ s = it }

The compiler complains: java.lang.String is not assignable from java.lang.String

In my understanding this is not the case mentioned in 
http://code.google.com/p/groovypptest/wiki/TypeInference#Variable_declaration 
so it should compile.

Original issue reported on code.google.com by adam%sch...@gtempaccount.com on 2 Apr 2011 at 5:12

GoogleCodeExporter commented 8 years ago
I was under the impression you can't reassign variable inside a closure in 
groovy++?

Original comment by wmacgy...@gmail.com on 2 Apr 2011 at 6:12

GoogleCodeExporter commented 8 years ago
Well, we definitely need better message but in general in Groovy++ (before we 
finished good inlining possibilities) it is not possible to change bound 
variable inside closure.

Original comment by alex.tka...@gmail.com on 2 Apr 2011 at 7:13

GoogleCodeExporter commented 8 years ago
OK, you can say that Groovy++ encourages a purely functional programming style 
;)

Original comment by adam%sch...@gtempaccount.com on 3 Apr 2011 at 10:53

GoogleCodeExporter commented 8 years ago
I wish we could say so but it would not be true. The main reason is that we can 
not (yet!) separate case when closure is effectively inline from the case when 
it can be passed to another thread. There are also performance considerations 
to make shared variable immutable.

Original comment by alex.tka...@gmail.com on 3 Apr 2011 at 12:00

GoogleCodeExporter commented 8 years ago
The snippet provided now seems to be failing with the clearer error message 
"Cannot modify final field TryGroovy$foo$1.s".

Original comment by roshanda...@gmail.com on 5 May 2011 at 5:14

GoogleCodeExporter commented 8 years ago

Original comment by alex.tka...@gmail.com on 24 May 2011 at 11:20