Implicit type conversion for Xbase [message #1421002] |
Wed, 10 September 2014 16:20  |
Eclipse User |
|
|
|
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 16:25] by Moderator
|
|
|
Re: Implicit type conversion for Xbase [message #1421067 is a reply to message #1421002] |
Wed, 10 September 2014 18:31   |
Eclipse User |
|
|
|
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 #1746103 is a reply to message #1746049] |
Fri, 21 October 2016 14:42  |
Eclipse User |
|
|
|
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.)
|
|
|
Powered by
FUDForum. Page generated in 0.29726 seconds