Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 365412 - [Xtend2] Cant use a Closure as Method Parameter
Summary: [Xtend2] Cant use a Closure as Method Parameter
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: 2.2.0   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: M6   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-02 06:12 EST by patrick.lehmann CLA
Modified: 2017-10-31 10:58 EDT (History)
1 user (show)

See Also:
sebastian.zarnekow: kepler+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.