Skip to main content


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 07:45 Go to next message
Axel Guckelsberger is currently offline Axel GuckelsbergerFriend
Messages: 347
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 07:46]

Report message to a moderator

Re: Confused about the map function [message #868556 is a reply to message #868547] Wed, 02 May 2012 08:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13669
Registered: July 2009
Senior Member
Hi you have to explicitly call flatten for iterables of iterable

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Confused about the map function [message #868562 is a reply to message #868556] Wed, 02 May 2012 08:22 Go to previous message
Axel Guckelsberger is currently offline Axel GuckelsbergerFriend
Messages: 347
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: Sat Dec 05 00:36:28 GMT 2020

Powered by FUDForum. Page generated in 0.01972 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top