[xcore/xbase] Referencing operation parameters from inside lambda expressions [message #1004041] |
Tue, 22 January 2013 13:41 |
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 14:13 |
Ed Merks Messages: 33147 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
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: [xcore/xbase] Referencing operation parameters from inside lambda expressions [message #1004125 is a reply to message #1004066] |
Tue, 22 January 2013 16:11 |
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
>
|
|
|
Powered by
FUDForum. Page generated in 0.03673 seconds