Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » create Predicates with XTend
create Predicates with XTend [message #800692] Fri, 17 February 2012 11:12 Go to next message
Alex G is currently offline Alex GFriend
Messages: 96
Registered: January 2012
Member
Hi!

I want to write a FilteringScope in XTend in order to filter out some objects from a parent scope. Therefore I need a Predicate.
Here is a small Java example:

return new FilteringScope(scope,
    new Predicate<IEObjectDescription>() {			
       public boolean apply(IEObjectDescription ieod) {
          return ...};
            });


If I write this in XTend and add the imports

import com.google.common.base.Predicate
import com.google.common.base.Predicates


this does not work for me:

val Predicate<IEObjectDescription> pred = new Predicate<IEObjectDescription>() {
   def boolean apply(IEObjectDescription ieod) {
      return true
   } 
}


Even if I write "override" instead of "def", XTend wants to change back to "def".
And the "predicate"-constructor is underlined with error message:
Couldn't resolve reference to JvmConstructor 'Predicate'.
possible Quickfix: add imports which I have already added (com.google.common.base.Predicate).

I tried to construct this predicate via the static functions in com.google.common.base.Predicates. First I filtered out

val filteredItems = selectCompatible(parentScope.allElements, something.eClass).toList


and then
val Predicate<IEObjectDescription> pred = Predicates::in(filteredItems)
val filtscope = new FilteringScope(parentScope, pred)


But this just does not work as assumed (my filtscope.allElements is an empty list and not equal to filteredItems).

Furthermore I don't know how I can use XTend Function which gives me (IEObjectDescription) => Boolean to a Predicate.

I would be very glad for some help.
Re: create Predicates with XTend [message #800715 is a reply to message #800692] Fri, 17 February 2012 11:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

what about using a closure

val filtscope = new FilteringScope(null, [iod|true])


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: create Predicates with XTend [message #800717 is a reply to message #800715] Fri, 17 February 2012 11:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
P.S:

Anonymous Inner Classes dont work in Xtend


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: create Predicates with XTend [message #802854 is a reply to message #800717] Mon, 20 February 2012 14:56 Go to previous messageGo to next message
Alex G is currently offline Alex GFriend
Messages: 96
Registered: January 2012
Member
Ah cool!
Didn't know that Function<T,Boolean> can somehow be transformed into a Predicate<T> internally depending on the parameter type of the calling method.
The statements in the XTend tutorial
http://www.eclipse.org/xtend/documentation/index.html#closures
do not indicate on this internal transformation.
Thank you!
Re: create Predicates with XTend [message #802897 is a reply to message #802854] Mon, 20 February 2012 16:18 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
"closures are automatically coerced to expected types if those types
declare only one method"

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 20.02.12 15:56, schrieb Alex G:
> Ah cool!
> Didn't know that Function<T,Boolean> can somehow be transformed into a
> Predicate<T> internally depending on the parameter type of the calling
> method.
> The statements in the XTend tutorial
> http://www.eclipse.org/xtend/documentation/index.html#closures
> do not indicate on this internal transformation.
> Thank you!
Previous Topic:Catch Unquoted String
Next Topic:support for Eclipse 3.7?
Goto Forum:
  


Current Time: Fri Apr 26 03:36:05 GMT 2024

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

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

Back to the top