Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » how to write (existing) java 8 lambda in xtend
how to write (existing) java 8 lambda in xtend [message #1787310] Wed, 23 May 2018 13:45 Go to next message
Christian Eugster is currently offline Christian EugsterFriend
Messages: 175
Registered: July 2009
Location: St. Gallen Switzerland
Senior Member
Hi
after consulting the xtend documentation and http://blog.efftinge.de/2012/12/java-8-vs-xtend.html
I am stuck with the following problem:

my original code is:
@Override
public Set<AccessionLink> getAccessionLinks(ElementState state)
{
	return this.accessionLinks.stream().filter(element -> state.check(element.isDeleted())).collect(Collectors.toSet());
}


accessionLinks is as HashSet of JpaAccessionLink (derived as subclass from AccessionLink), ElementState is an enum that does a check if the element has to be filtered out. The return value should be converted from Set<JpaAccessionLink> to Set<AccessionLink>. I tried with

override Set<JpaAccessionLink> getAccessionLinks(ElementState state)
{
	accessionLinks.stream.filter(e | state.check(deleted)).collect(Collectors.toSet)
}

But that is not the solution I am looking for because I need to override the given method and so must give a Set<AccessionLink> as return value. I had other tries too, but did not succeed. I would be glad, if someone could give me a solution to the problem. Thank you
Re: how to write (existing) java 8 lambda in xtend [message #1787317 is a reply to message #1787310] Wed, 23 May 2018 15:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12243
Registered: July 2009
Senior Member
did you try something like #[1,2,3,4].stream.filter[e|e%2==0].collect(Collectors::toSet)

Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to write (existing) java 8 lambda in xtend [message #1787367 is a reply to message #1787317] Thu, 24 May 2018 07:34 Go to previous messageGo to next message
Christian Eugster is currently offline Christian EugsterFriend
Messages: 175
Registered: July 2009
Location: St. Gallen Switzerland
Senior Member
Hi

I tried this one

override Set<DescriptionAreaLink> getDescriptionAreaLinks(ElementState state)
{
	return this.descriptionAreaLinks.stream.filter[e | state.check(e.isDeleted())].collect(Collectors::toSet);
}


but the editor complains, that the return value of Set<JpaDescriptionAreaLink> does not match the expected Set<DescriptionAreaLink>. The set descriptionAreaLinks is a Set<JpaDescriptionAreaLinks> and JpaDescriptionAreaLink is a subclass of DescriptionAreaLink. In java there I have no problem with it as it seems that java handles this internally. How can I achieve this with lambdas in xtend?

I want to say that it is really a big pleasure to work with xtend. I like it very much. Congratulations for this work!

Christian

[Updated on: Thu, 24 May 2018 07:35]

Report message to a moderator

Re: how to write (existing) java 8 lambda in xtend [message #1787382 is a reply to message #1787367] Thu, 24 May 2018 11:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12243
Registered: July 2009
Senior Member
you can do simply a filter(Type) on the set

Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to write (existing) java 8 lambda in xtend [message #1787395 is a reply to message #1787382] Thu, 24 May 2018 16:33 Go to previous messageGo to next message
Christian Eugster is currently offline Christian EugsterFriend
Messages: 175
Registered: July 2009
Location: St. Gallen Switzerland
Senior Member
The following proofed to not being a solution! Same problem as in the beginning of this thread.

After further trials the solution is a lot easier than thought:

authorities.stream.filter[e | state.check(e.deleted)].collect(Collectors.toSet) as Set<Authority>


Thanks and have a nice evening!
Christian

[Updated on: Fri, 25 May 2018 06:25]

Report message to a moderator

Re: how to write (existing) java 8 lambda in xtend [message #1787421 is a reply to message #1787395] Fri, 25 May 2018 07:29 Go to previous messageGo to next message
Christian Eugster is currently offline Christian EugsterFriend
Messages: 175
Registered: July 2009
Location: St. Gallen Switzerland
Senior Member
Say we have an Interface IFoo and an implementation of it named Foo.
interface IFoo
{
   def List<IBar> getBars()
}

class Foo extends IFoo
{
   List<Bar> barList = New ArrayList()

   override getBars()
   {
      barList.stream.collect(Collectors.toList)
   }
}

Interface IBar
{}

class Bar implements IBar {}

How can i achieve implemented Method to Return List<IBar> instead of List<Bar> ?
Re: how to write (existing) java 8 lambda in xtend [message #1787431 is a reply to message #1787421] Fri, 25 May 2018 09:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12243
Registered: July 2009
Senior Member
why do you use stream api at all?

barList.stream.collect(Collectors.toList).filter(IBar).toList


Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: how to write (existing) java 8 lambda in xtend [message #1787435 is a reply to message #1787431] Fri, 25 May 2018 10:13 Go to previous message
Christian Eugster is currently offline Christian EugsterFriend
Messages: 175
Registered: July 2009
Location: St. Gallen Switzerland
Senior Member
Ok that was it definitively, many thanks!
Previous Topic:interface default method: how to implement
Next Topic:Errors creating Xtext Project From Existing Ecore Models
Goto Forum:
  


Current Time: Wed Sep 26 03:03:26 GMT 2018

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

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

Back to the top