Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 352138

Summary: [xtend] Uncompilable code when using loop variable in a function
Product: [Modeling] TMF Reporter: Karsten Thoms <karsten.thoms>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: sebastian.zarnekow
Version: 2.0.0Flags: sebastian.zarnekow: indigo+
Target Milestone: SR1   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Karsten Thoms CLA 2011-07-14 13:01:28 EDT
This Xtend code produces uncompilable code:
=============================================
for (e : model.entities) {
  val p = model.configuration.classes.findFirst(c|c.entityName==e.name)
  e.persistentClass = p
}
=============================================


"e" is in this case not final, but used within a function:
"Cannot refer to a non-final variable e inside an inner class defined in a different method"
=============================================
      for (Entity e : _entities) {
        {
          Configuration _configuration = this.model.getConfiguration();
          EList<PersistentClass> _classes = _configuration.getClasses();
          final Function1<PersistentClass,Boolean> _function = new Function1<PersistentClass,Boolean>() {
              public Boolean apply(final PersistentClass c) {
                String _entityName = c.getEntityName();

                String _name = e.getName();   <== *** ERROR

                boolean _operator_equals = ObjectExtensions.operator_equals(_entityName, _name);
                return ((Boolean)_operator_equals);
              }
            };
          PersistentClass _findFirst = IterableExtensions.<PersistentClass>findFirst(_classes, _function);
          final PersistentClass p = _findFirst;
          e.setPersistentClass(p);
        }
      }
=============================================


Workaround: Create a val for the loop variable:
=============================================
for (e : model.entities) {
  val e1 = e
  val p = model.configuration.classes.findFirst(c|c.entityName==e1.name)
  e1.persistentClass = p
}
=============================================


Maybe the loop's variable should be compiled as final?
Comment 1 Sebastian Zarnekow CLA 2011-07-14 13:03:09 EDT
This one was already fixed for SR1
Comment 2 Karsten Thoms CLA 2017-09-19 17:06:16 EDT
Closing all bugs that were set to RESOLVED before Neon.0
Comment 3 Karsten Thoms CLA 2017-09-19 17:18:09 EDT
Closing all bugs that were set to RESOLVED before Neon.0