Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 370302 - [xbase] Unexpected type error
Summary: [xbase] Unexpected type error
Status: CLOSED FIXED
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: 2.3.0   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: RC1   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-01 03:33 EST by Sebastian Zarnekow CLA
Modified: 2017-10-31 11:25 EDT (History)
2 users (show)

See Also:
sebastian.zarnekow: juno+


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Zarnekow CLA 2012-02-01 03:33:17 EST
This works:

  Set<String> strings
  static (int,int)=>int min = [i,j | if (i < j) i else j]
  
  def go() {
    strings.map[index].reduce(min)
  }
  
  def index(String s) {
    s.hashCode
  }

But this does not work:

  Set<String> strings
  static (int,int)=>int min = [i,j | if (i < j) i else j]
  static (String)=>int index = [s | s.hashCode]
  
  def go() {
    // Incompatible receiver type.
    // Expected java.lang.Iterable<java.lang.Integer>
    // or java.lang.Integer[] or int[]
    // but was java.lang.Iterable<? extends java.lang.Integer>
    strings.map(index).reduce(min)
  }

We have to double check the declaration of #reduce. 
It may have to be 

<T> T reduce(Iterable<? extends T> iterable, Function2<? super T, ? super T, ? extends T> function)
instead of 

<T> T reduce(Iterable<T> iterable, Function2<? super T, ? super T, ? extends T> function). 

Same for #fold?
Comment 1 Moritz Eysholdt CLA 2012-05-21 07:59:39 EDT
#fold works without problems ----
Set<String> strings
static (int,int)=>int min = [i,j | if (i < j) i else j]
static (String)=>int index = [s | s.hashCode]

def go() {
  strings.map(index).fold(0,min)
}
----

This is probably because the signature of fold 
<T, R> R fold(Iterable<T> iterable, R seed, Function2<? super R, ? super T, ? extends R> function) 
does not T as the Function2's return type but R.
Comment 2 Moritz Eysholdt CLA 2012-05-21 08:43:51 EDT
The failing scenarion fails in Java as well:

public static void main(final String[] args) {
  Iterable<? extends Integer> ints = null;
  Functions.Function2<Integer, Integer, Integer> func = null;
  IterableExtensions.reduce(ints, func);
}

This fails for reduce(Iterable<T> iterable,...) but not for reduce(Iterable<? extends T> iterable,...)
Comment 4 Moritz Eysholdt CLA 2012-05-21 08:52:00 EDT
as I said already: fixed.
Comment 5 Moritz Eysholdt CLA 2012-05-22 04:54:11 EDT
I've reverted the commit since it caused a scenarion on one of our example projectes that the type system could not yet handle.

The commit can be re-applied via "git cherry-pick -x 2f8aef90139f088cd69b7c77a0ea334e162e1f3d"
Comment 6 Sebastian Zarnekow CLA 2012-05-22 05:00:48 EDT
Finally the Xtend equivalent to this one should succeed:

public static void main(final String[] args) {
	Iterable<? extends Integer> ints1 = null;
	Iterable<Integer> ints2 = null;
	Functions.Function2<? super Integer, ? super Integer, ? extends Integer> func1 = null;
	Functions.Function2<? super Integer, Integer, ? extends Integer> func2 = null;
	Functions.Function2<Integer, ? super Integer, ? extends Integer> func3 = null;
	Functions.Function2<Integer, Integer, ? extends Integer> func4 = null;
	Functions.Function2<? super Integer, ? super Integer, Integer> func5 = null;
	Functions.Function2<? super Integer, Integer, Integer> func6 = null;
	Functions.Function2<Integer, ? super Integer, Integer> func7 = null;
	Functions.Function2<Integer, Integer, Integer> func8 = null;
	reduce(ints1, func1);
	reduce(ints1, func2);
	reduce(ints1, func3);
	reduce(ints1, func4);
	reduce(ints1, func5);
	reduce(ints1, func6);
	reduce(ints1, func7);
	reduce(ints1, func8);
	reduce(ints2, func1);
	reduce(ints2, func2);
	reduce(ints2, func3);
	reduce(ints2, func4);
	reduce(ints2, func5);
	reduce(ints2, func6);
	reduce(ints2, func7);
	reduce(ints2, func8);
}
Comment 7 Moritz Eysholdt CLA 2012-05-22 09:27:57 EDT
reapplied the patch, since the error in the examples was caused by something else. A testcase for this still missing.
Comment 9 Sebastian Zarnekow CLA 2013-02-14 04:46:40 EST
Example from comment #6 works in Xtend.
Comment 10 Eclipse Webmaster CLA 2017-10-31 11:25:00 EDT
Requested via bug 522520.

-M.