Community
Participate
Working Groups
Note: This is from RC3, I think, but I couldn't find this in Bugzilla, and it doesn't look like it's fixes in Git. -- First, I found what I thought to be a bug in Xtend2: class LoopVarFinal { def problem_1() ''' «FOR i:1..10» «FOR j:(1..10).map(k | k*i) SEPARATOR " ... "»«j»«ENDFOR» «ENDFOR» ''' ) This translates into invalid code, since the 'for' variable is not final, and so won't be usable in the anonymous function passed into 'map'. The workaround: class LoopVarFinal { def problem_1() ''' «FOR i:1..10» «val final_i = i» «FOR j:(1..10).map(k | k*final_i) SEPARATOR " ... "»«j»«ENDFOR» «ENDFOR» ''' ---- Sure enough, the generated code for "for" omits the final qualifier for the loop variable. Same problem exists in 'straight' Xbase (still in Xpand): def problem_2() { for (i : 1..10) { for (j : (1..10).map(k | k*i)) { print(" " + j + " "); } } } But! The problem is really bigger than that: var lim = 37; val numbers = (1..100).filter(i | i <= lim); So, formals and vals are final, but vars and for-loop variables are not. IMHO, the user should not have to care: Non-final free variables in anonymous functions should be frozen as finals at the place of function initialization -- OR -- the problem should be detected and checked (if fixed for 'for-loops' the problem should be limited to 'var'). Applicable versions: org.eclipse.xtext.xbase (2.0.0.v201106010845) "Xbase Model" [Active] org.eclipse.xtext.xbase.doc (2.0.0.v201106010845) "Xbase Language's User Guide" [Resolved] org.eclipse.xtext.xbase.junit (2.0.0.v201106010845) "Xbase Junit support" [Resolved] org.eclipse.xtext.xbase.lib (2.0.0.v201106010845) "Xbase Runtime Library" [Resolved] org.eclipse.xtext.xbase.ui (2.0.0.v201106010845) "Xbase UI" [Active] org.eclipse.xtext.xtend2 (2.0.0.v201106010845) "Xtend2 Model" [Active] org.eclipse.xtext.xtend2.doc (2.0.0.v201106010845) "Xtend2 Language's User Guide" [Resolved] org.eclipse.xtext.xtend2.lib (2.0.0.v201106010845) "Xtend2 Runtime Library" [Resolved] org.eclipse.xtext.xtend2.ui (2.0.0.v201106010845) "Xtend2 UI" [Active] FWIW: Xtext rocks, thanks guys!
Oops, I found the note in the Xbase language spec noting that only final variables and parameters are available in the scope of the anonymous function. If so, an error message would be in order, and my suggestion on copying mutable free variables into finals may be ignored (or considered, but not as an error). And for should still bind to a value. Would you be interested in tests + patches?
Thanks for the detailled bug report.
Pushed to master.
Fix is great, thank you