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

Bug 370232

Summary: Casts don't appear to coerce closures
Product: [Tools] Xtend Reporter: William R. Burdick Jr. <bill>
Component: CoreAssignee: Project Inbox <xtend-inbox>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P3 CC: dennis.huebner, moritz.eysholdt, sebastian.zarnekow, sven.efftinge
Version: 2.3.0   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 376037    
Bug Blocks:    

Description William R. Burdick Jr. CLA 2012-01-31 10:43:04 EST
Build Identifier: 

Closures don't get type information from a cast.

This works:

var Function<Integer,Integer> s = [x|x * 2]


but this doesn't:

var s = [x|x * 2] as Function<Integer,Integer>

Reproducible: Always

Steps to Reproduce:
1. try to compile this declaraion:

var s = [x|x * 2] as Function<Integer,Integer>
Comment 1 Sven Efftinge CLA 2012-01-31 11:04:03 EST
*** Bug 370173 has been marked as a duplicate of this bug. ***
Comment 2 Sven Efftinge CLA 2012-01-31 11:05:10 EST
Casts are generally not used in expected type inference. See also : http://groups.google.com/group/xtend-lang/browse_thread/thread/63a2181439fef757
Comment 3 William R. Burdick Jr. CLA 2012-02-01 12:02:20 EST
This becomes more important when closures can coerce to abstract classes, because you may have several abstract classes implementing the same interface.

With your patch to allow casts to work for this, I can now choose an abstract class by casting a literal closure.  I have a closure-compatible abstract class (given my patch), called SimpleSelectionAdapter, that implements SWT's SelectionListener interface and leaves only widgetSelected() abstract and I can use a closure for it, like this:


menuItem.addSelectionListener([e|...] as SimpleSelectionAdapter)


If the cast doesn't work, I need to declare a new variable for this, which seems counter to Xtend's focus on programmer convenience.
Comment 4 Sven Efftinge CLA 2012-04-04 02:25:16 EDT
Something we should look at while reworking the linking / type system
Comment 5 Dennis Huebner CLA 2013-02-14 04:35:44 EST
Is an enhancement.
Comment 6 Sebastian Zarnekow CLA 2013-03-12 09:06:19 EDT
see also bug 403032
Comment 7 Sven Efftinge CLA 2013-03-12 09:07:07 EDT
*** Bug 403032 has been marked as a duplicate of this bug. ***