Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 349762 - Inconsistent use of finals in Xtext and Xtend2
Summary: Inconsistent use of finals in Xtext and Xtend2
Status: VERIFIED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: 2.0.0   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: SR1   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-06-18 19:33 EDT by Jesper Moller CLA
Modified: 2011-11-07 10:19 EST (History)
1 user (show)

See Also:
sebastian.zarnekow: indigo+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jesper Moller CLA 2011-06-18 19:33:25 EDT
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!
Comment 1 Jesper Moller CLA 2011-06-18 19:41:45 EDT
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?
Comment 2 Sebastian Zarnekow CLA 2011-06-19 04:31:20 EDT
Thanks for the detailled bug report.
Comment 3 Sebastian Zarnekow CLA 2011-06-22 05:24:51 EDT
Pushed to master.
Comment 4 Jesper Moller CLA 2011-11-07 10:19:47 EST
Fix is great, thank you