Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [xcore/xbase] Referencing operation parameters from inside lambda expressions
[xcore/xbase] Referencing operation parameters from inside lambda expressions [message #1004041] Tue, 22 January 2013 08:41 Go to next message
Frank Grimm is currently offline Frank Grimm
Messages: 22
Registered: July 2009
Junior Member
Hi newsgroup,

In case my question should go to the Xtext/Xbase newsgroup, I apologise
for posting to the wrong newsgroup.

In an Xcore class's operation, I'm trying to reference the operation's
parameter in a lambda expression like so:

class Test
{
op void test(EClass theEClass)
{

theEClass.getEAllReferences().findFirst[getEReferenceType().isSuperTypeOf(theEClass)]
}
}

The Xcore file looks fine (no errors). The generated Java code however
does not compile because parameter theEClass is not final an cannot be
accessed from inside the anonymous inner class generated for the lambda:

public void test(EClass theEClass)
{
EList<EReference> _eAllReferences = theEClass.getEAllReferences();
final Function1<EReference,Boolean> _function = new
Function1<EReference,Boolean>()
{
public Boolean apply(final EReference it)
{
EClass _eReferenceType = it.getEReferenceType();
// ERROR: Cannot refer to a non-final variable theEClass inside an
inner class defined in a
// different method
boolean _isSuperTypeOf =
_eReferenceType.isSuperTypeOf(theEClass);
return Boolean.valueOf(_isSuperTypeOf);
}
};
IterableExtensions.<EReference>findFirst(_eAllReferences, _function);
}

I can work around this problem by defining a val:

op void test(EClass theEClass)
{
val theFinalEClass = theEClass
theEClass.getEAllReferences().
findFirst[getEReferenceType().isSuperTypeOf(theFinalEClass)]
}

theFinalEClass will then become a final variable in the generated Java
source code.

I'm using Xcore 1.0.1.v20120917. I'm, however, not sure whether this is
an Xcore issue or rather a Xbase issue or maybe I'm using lambdas in the
wrong way in the first place...

Thank you and kind regards,
Frank
Re: [xcore/xbase] Referencing operation parameters from inside lambda expressions [message #1004066 is a reply to message #1004041] Tue, 22 January 2013 09:13 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26012
Registered: July 2009
Senior Member
Frank,

In the Xcore 1.1 stream (i.e., the EMF 2.9 stream), the generator
automatically makes the parameters of an operation with a body final.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=390744

There are many fixes in the latest stream, so you're definitely best of
to install that version.


On 22/01/2013 2:41 PM, Frank Grimm wrote:
> Hi newsgroup,
>
> In case my question should go to the Xtext/Xbase newsgroup, I apologise
> for posting to the wrong newsgroup.
>
> In an Xcore class's operation, I'm trying to reference the operation's
> parameter in a lambda expression like so:
>
> class Test
> {
> op void test(EClass theEClass)
> {
>
> theEClass.getEAllReferences().findFirst[getEReferenceType().isSuperTypeOf(theEClass)]
> }
> }
>
> The Xcore file looks fine (no errors). The generated Java code however
> does not compile because parameter theEClass is not final an cannot be
> accessed from inside the anonymous inner class generated for the lambda:
>
> public void test(EClass theEClass)
> {
> EList<EReference> _eAllReferences = theEClass.getEAllReferences();
> final Function1<EReference,Boolean> _function = new
> Function1<EReference,Boolean>()
> {
> public Boolean apply(final EReference it)
> {
> EClass _eReferenceType = it.getEReferenceType();
> // ERROR: Cannot refer to a non-final variable theEClass inside an
> inner class defined in a
> // different method
> boolean _isSuperTypeOf =
> _eReferenceType.isSuperTypeOf(theEClass);
> return Boolean.valueOf(_isSuperTypeOf);
> }
> };
> IterableExtensions.<EReference>findFirst(_eAllReferences, _function);
> }
>
> I can work around this problem by defining a val:
>
> op void test(EClass theEClass)
> {
> val theFinalEClass = theEClass
> theEClass.getEAllReferences().
> findFirst[getEReferenceType().isSuperTypeOf(theFinalEClass)]
> }
>
> theFinalEClass will then become a final variable in the generated Java
> source code.
>
> I'm using Xcore 1.0.1.v20120917. I'm, however, not sure whether this is
> an Xcore issue or rather a Xbase issue or maybe I'm using lambdas in the
> wrong way in the first place...
>
> Thank you and kind regards,
> Frank
Re: [xcore/xbase] Referencing operation parameters from inside lambda expressions [message #1004125 is a reply to message #1004066] Tue, 22 January 2013 11:11 Go to previous message
Frank Grimm is currently offline Frank Grimm
Messages: 22
Registered: July 2009
Junior Member
Thanks Ed, switching to the Xcore 1.1 stream did the trick.

On 22.01.2013 15:13, Ed Merks wrote:
> Frank,
>
> In the Xcore 1.1 stream (i.e., the EMF 2.9 stream), the generator
> automatically makes the parameters of an operation with a body final.
>
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=390744
>
> There are many fixes in the latest stream, so you're definitely best of
> to install that version.
>
>
> On 22/01/2013 2:41 PM, Frank Grimm wrote:
>> Hi newsgroup,
>>
>> In case my question should go to the Xtext/Xbase newsgroup, I apologise
>> for posting to the wrong newsgroup.
>>
>> In an Xcore class's operation, I'm trying to reference the operation's
>> parameter in a lambda expression like so:
>>
>> class Test
>> {
>> op void test(EClass theEClass)
>> {
>>
>> theEClass.getEAllReferences().findFirst[getEReferenceType().isSuperTypeOf(theEClass)]
>>
>> }
>> }
>>
>> The Xcore file looks fine (no errors). The generated Java code however
>> does not compile because parameter theEClass is not final an cannot be
>> accessed from inside the anonymous inner class generated for the lambda:
>>
>> public void test(EClass theEClass)
>> {
>> EList<EReference> _eAllReferences = theEClass.getEAllReferences();
>> final Function1<EReference,Boolean> _function = new
>> Function1<EReference,Boolean>()
>> {
>> public Boolean apply(final EReference it)
>> {
>> EClass _eReferenceType = it.getEReferenceType();
>> // ERROR: Cannot refer to a non-final variable theEClass inside an
>> inner class defined in a
>> // different method
>> boolean _isSuperTypeOf =
>> _eReferenceType.isSuperTypeOf(theEClass);
>> return Boolean.valueOf(_isSuperTypeOf);
>> }
>> };
>> IterableExtensions.<EReference>findFirst(_eAllReferences,
>> _function);
>> }
>>
>> I can work around this problem by defining a val:
>>
>> op void test(EClass theEClass)
>> {
>> val theFinalEClass = theEClass
>> theEClass.getEAllReferences().
>> findFirst[getEReferenceType().isSuperTypeOf(theFinalEClass)]
>> }
>>
>> theFinalEClass will then become a final variable in the generated Java
>> source code.
>>
>> I'm using Xcore 1.0.1.v20120917. I'm, however, not sure whether this is
>> an Xcore issue or rather a Xbase issue or maybe I'm using lambdas in the
>> wrong way in the first place...
>>
>> Thank you and kind regards,
>> Frank
>
Previous Topic:EMF Ecore output xmi, can have only text content element ?
Next Topic:How open EMF generated editor when model is not in a file?
Goto Forum:
  


Current Time: Thu Aug 28 05:18:55 EDT 2014

Powered by FUDForum. Page generated in 0.02110 seconds