Bug 391758 - Type inference too greedy
Summary: Type inference too greedy
Status: REOPENED
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.4.0   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on: 376037
Blocks:
  Show dependency tree
 
Reported: 2012-10-12 06:38 EDT by Stefan Oehme CLA
Modified: 2013-05-27 06:07 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stefan Oehme CLA 2012-10-12 06:38:02 EDT
The problem is most easily explained with an example. Let's take the fold-Function:

<T, R> fold(Iterable<T> iterable, R seed, (R,T)=>R function)

When I write 

iterable.fold(anArrayList)[functionReturningList]

the compiler complains that my function should return ArrayList instead. I.e. the seed wins when determining R. To get around this I have to explicitly cast anArrayList to List. This ruins an otherwise readable line of code.

I worked around this by defining my own version of fold like so:

<T, R, S extends R> fold(Iterable<T> iterable, S seed, (R,T)=>R function)

However, the compiler should be able to correctly determine R without a third type variable.
Comment 1 Sebastian Zarnekow CLA 2012-10-12 07:06:11 EDT
Thanks for the report. I can reproduce the issue.
Comment 2 Sven Efftinge CLA 2013-03-06 05:10:07 EST
The following works now.

val (List<String>,String)=>List<String> functionReturningList = [a, b| 
  a += b
  return a
]
#['foo'].fold(newArrayList, functionReturningList)

added a test case.
Comment 3 Sebastian Zarnekow CLA 2013-03-06 05:14:02 EST
This is still an issue with the more natural style

strings.fold(newArrayList) [ list , b | 
  list += b
  return list.sublist(1)
]