Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Implicit type conversion for Xbase
Implicit type conversion for Xbase [message #1421002] Wed, 10 September 2014 20:20 Go to next message
Cioran Naroic is currently offline Cioran NaroicFriend
Messages: 18
Registered: December 2013
Junior Member
Hi everyone,

I'm trying to implement a little tricky extension for Xbase. Given the following code:

val a = new A    // a.toC() yields a C
val b = new B    // b.toC() yields a C

// Doesn't work: Type mismatch
function_that_accepts_C_as_argument(a) // passing a.toC() would work, but I want it to be implicit

// Doesn't work: Type mismatch
function_that_accepts_C_as_argument(b) // passing b.toC() would work, but I want it to be implicit


A and B aren't subtypes of C, so I can't call the function_that_accept_... directly. However, I know that they contains the toC() method that yields a C, so everything will be ok if the compiler adds the toC() automatically.

More generally, I would like to be able to tell something like "I know this looks weird, but let me try" to the Xbase typesystem/validator, and override the compiler to add the ".toC()" in the generated Java code.

Even more generally, I would like to be able to detect any kind of type mismatch in Xbase code and explore the context to see if I got the appropriate toX() method.

Do you have some ideas on where and how I should override the Xbase typesystem / validator / compiler to implement this? Just pointing the relevant classes in the API would help me a lot.

Thanks a lot,

Cioran

[Updated on: Wed, 10 September 2014 20:25]

Report message to a moderator

Re: Implicit type conversion for Xbase [message #1421067 is a reply to message #1421002] Wed, 10 September 2014 22:31 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3095
Registered: July 2009
Senior Member
Hey Cioran,

you may want to override
org.eclipse.xtext.xbase.typesystem.computation.SynonymTypesProvider and
provide custom conversions there. You don't get the expected type but
the original type which can be explored to find all toX methods.

In the compiler, look for internalToConvertedExpression(..) where you
get the expected type and the actual type. There you can insert the #toX
calls into the generated code.

Regards,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 10.09.14 22:20, schrieb Cioran Naroic:
> Hi everyone,
>
> I'm trying to implement a little tricky extension for Xbase. Given the
> following code:
>
>
> val a = new A // a.toC() yields a C
> val b = new B // b.toC() yields a C
>
> // Doesn't work
> function_that_accepts_C_as_argument(a) // passing a.toC() would work,
> but I want it to be implicit
>
> // Doesn't work
> function_that_accepts_C_as_argument(b) // passing b.toC() would work,
> but I want it to be implicit
>
>
> A and B aren't subtypes of C, so I can't call the
> function_that_accept_... directly. However, I know that they contains
> the toC() method that yields a C, so everything will be ok if the
> compiler adds the toC() automatically.
>
> More generally, I would like to be able to tell something like "I know
> this looks weird, but let me try" to the Xbase typesystem/validator, and
> override the compiler to add the ".toC()" in the generated Java code.
>
> Even more generally, I would like to be able to detect any kind of type
> mismatch in Xbase code and explore the context to see if I got the
> appropriate toX() method.
>
> Do you have some ideas on where and how I should override the Xbase
> typesystem / validator / compiler to implement this?
>
> Thanks a lot,
>
> Cioran
>
Re: Implicit type conversion for Xbase [message #1421368 is a reply to message #1421067] Thu, 11 September 2014 09:15 Go to previous messageGo to next message
Cioran Naroic is currently offline Cioran NaroicFriend
Messages: 18
Registered: December 2013
Junior Member
Just the two pointers I needed. Still a long way to go for a clean implementation but the proof-of-concept works. Thank you a lot Sebastian!

Re: Implicit type conversion for Xbase [message #1746046 is a reply to message #1421368] Thu, 20 October 2016 19:09 Go to previous messageGo to next message
Davor Cubranic is currently offline Davor CubranicFriend
Messages: 3
Registered: July 2009
Junior Member
Sorry to revive an old thread, but I'm trying to do exactly this and failing, even after following Sebastian's suggestion.

In my case, I have a class MyNumber that can be converted to Double via "doubleValue" method. I have a custom synonym provider which announces the primitive double as the synonym for MyNumber. The custom compiler overrides internalToConvertedExpression and appends ".doubleValue" when the expected type is double. Both of these methods get executed, but the code generated for an expression like "myNumber - 5" is actually "((this.age.doubleValue()).MyNumberValue() - 5)". I can see where "MyNumberValue" gets added (in TypeConvertingCompiler.convertWrapperToPrimitive), but am not sure why the compiler ends up in that method despite my customizations.

I have a project repo that reproduces this at: https://github.com/cubranic/implicit-type-conversion
The 'master' branch calls the conversion MyNumber.doubleValue explicitly, while 'implicit-type-conversion' has the customized synonym provider and compiler that should auto-generate the conversion code.
Re: Implicit type conversion for Xbase [message #1746049 is a reply to message #1746046] Thu, 20 October 2016 19:47 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11087
Registered: July 2009
Senior Member

The super call is your problem
I'd rather hoook into doConversion and do something similar to wrapper to primitive



Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Implicit type conversion for Xbase [message #1746103 is a reply to message #1746049] Fri, 21 October 2016 18:42 Go to previous message
Davor Cubranic is currently offline Davor CubranicFriend
Messages: 3
Registered: July 2009
Junior Member
Christian, thank you very much! That was exactly it!

If there is a cleaner way to do this kind of thing with type synonyms, please do let me know. (For instance, it seems like if Xbase knew that MyNumber was just a wrapper for "double" primitive, I wouldn't have needed to customize the compiler.)
Previous Topic:Inconsistent formatting observed in using Xtext formatter
Next Topic:xcore, ecore, emf, xtext and maven
Goto Forum:
  


Current Time: Tue Aug 22 21:14:57 GMT 2017

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

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