Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Confused about the map function(Replacing Xtend collect in Xtend2)
Confused about the map function [message #868547] Wed, 02 May 2012 03:45 Go to next message
Axel Guckelsberger is currently offline Axel Guckelsberger
Messages: 183
Registered: July 2009
Senior Member
Hi all,

during migration from Xtend/Xpand to Xtend2 I have the following problem with the new collect syntax.

Let's say we have a World which contains multiple Country elements. Every country contains multiple Company elements.

If I do

    def getSortedCompanies(World it) {
    	getAllCompanies.sortBy(e|e.sortOrder)
    }

    def getAllCompanies(World it) {
        countries.map(e|e.companies)
    }


the field sortOrder is not visible in the first rule.

If I do

    def getAllCompanies(World it) {
        countries.map(e|e.companies) as Iterable<Company>
    }


I get an error message in the generated Java file as conversion from List<EList<Company>> to Iterable<Company> is not possible.

This is why I did

    def getAllCompanies(World it) {
        countries.map(e|e.companies) as List<Company>
    }


But when trying to run the Xtend generator it crashes with:

Caused by: java.lang.ClassCastException: org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList cannot be cast to ....Company


I think the problem is that I have a List of EList elements while I should have only a List.
What am I overlooking?

TIA
Axel

[Updated on: Wed, 02 May 2012 03:46]

Report message to a moderator

Re: Confused about the map function [message #868556 is a reply to message #868547] Wed, 02 May 2012 04:06 Go to previous messageGo to next message
Christian Dietrich is currently online Christian Dietrich
Messages: 4419
Registered: July 2009
Senior Member
Hi you have to explicitly call flatten for iterables of iterable
Re: Confused about the map function [message #868562 is a reply to message #868556] Wed, 02 May 2012 04:22 Go to previous message
Axel Guckelsberger is currently offline Axel Guckelsberger
Messages: 183
Registered: July 2009
Senior Member
Thanks Christian.

This version works:

    def getAllCompanies(World it) {
        countries.map(e|e.companies).flatten.toList as List<Country>
    }
Previous Topic:EOF not matching RULE_EOL
Next Topic:How accessing file complete location ?
Goto Forum:
  


Current Time: Thu May 23 15:33:48 EDT 2013

Powered by FUDForum. Page generated in 0.04903 seconds