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

Bug 365412

Summary: [Xtend2] Cant use a Closure as Method Parameter
Product: [Modeling] TMF Reporter: patrick.lehmann
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: sebastian.zarnekow
Version: 2.2.0Flags: sebastian.zarnekow: kepler+
Target Milestone: M6   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:

Description patrick.lehmann CLA 2011-12-02 06:12:00 EST
Build Identifier: Build id: 20110916-0149

Defining a closure as a parameter of a method leads to a compile error in the generated java code.

Reproducible: Always

Steps to Reproduce:
Sample Xtend2 File:

import java.util.ArrayList
import java.util.List

class XtendCheck {
	
	def test() {	
		val foos = new ArrayList<String>() // add some Foos
		foos.expand(s|s.empty)		
	}
	
	def expand(List<String> list, (String) => Boolean filter) {
		val filteredList = list.filter(filter)
	}
}

Error:
The parameterized method <String>filter(Iterable<String>, Functions.Function1<? super String,Boolean>) of type IterableExtensions is not applicable for the arguments (List<String>, Functions.Function1<capture#5-of ? super String,capture#6-of ? extends Boolean>)
Comment 1 patrick.lehmann CLA 2011-12-02 06:30:17 EST
Workaround: use Functions$Function1<Property,Boolean> as Type of the Closure
Comment 2 Sebastian Zarnekow CLA 2013-04-19 05:40:40 EDT
Resolved.

Produced Java code is

import java.util.ArrayList;
import java.util.List;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

@SuppressWarnings("all")
public class XtendCheck {
  public void test() {
    ArrayList<String> _arrayList = new ArrayList<String>();
    final ArrayList<String> foos = _arrayList;
    final Function1<String,Boolean> _function = new Function1<String,Boolean>() {
        public Boolean apply(final String s) {
          boolean _isEmpty = s.isEmpty();
          return Boolean.valueOf(_isEmpty);
        }
      };
    this.expand(foos, _function);
  }
  
  public void expand(final List<String> list, final Function1<? super String,? extends Boolean> filter) {
    final Iterable<String> filteredList = IterableExtensions.<String>filter(list, (Function1<? super String,Boolean>)filter);
  }
}
Comment 3 Eclipse Webmaster CLA 2017-10-31 10:47:43 EDT
Requested via bug 522520.

-M.
Comment 4 Eclipse Webmaster CLA 2017-10-31 10:58:46 EDT
Requested via bug 522520.

-M.