Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 352138 - [xtend] Uncompilable code when using loop variable in a function
Summary: [xtend] Uncompilable code when using loop variable in a function
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: 2.0.0   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: SR1   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-14 13:01 EDT by Karsten Thoms CLA
Modified: 2017-09-19 17:18 EDT (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 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