Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [xbase] value coercion
[xbase] value coercion [message #714272] Wed, 10 August 2011 08:01 Go to next message
Eclipse UserFriend
Originally posted by:

Hi,

I wonder if there is a mechanism for providing rules to xbase for
coercing values, when types don't match (not unlike Scala's implicits).
So instead of getting error markers that some expression has the wrong
type, the error is silently ignored (or perhaps with an info marker), as
if it was rewritten/wrapped to provide a value of the proper type.

This is particularly convenient for functions that take functions as
arguments. E.g. a Class could be coerced to Predicate<Object> to test
for instanceof or a String could be coerced to Predicate<String> for
regexp matching.

Hallvard
Re: [xbase] value coercion [message #714299 is a reply to message #714272] Wed, 10 August 2011 08:59 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Hallvard,

there is the SynonymTypeProvider which allows to define conversions
statically. That is, int is synonym to Integer or Object[] is synonym to
List<Object>. Xtext 2.1 will likely provide such a feature on the type
reference level (XSynonymTypeReference). This would allow to do things
like (pseudocode):

_type(XStringLiteral literal, ..) {
String value = literal.value;
if (value.length == 1) {
if (value contains only digits) {
return synonymtype(typeof(String), typeof(Number), typeof(char));
} else {
return synonymtype(typeof(String), typeof(char));
}
} else if (value contains only digits) {
return synonymtype(typeof(String), typeof(Number))
} else {
return typeof(String);
}
}

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

On 10.08.11 10:01, Hallvard Trætteberg wrote:
> Hi,
>
> I wonder if there is a mechanism for providing rules to xbase for
> coercing values, when types don't match (not unlike Scala's implicits).
> So instead of getting error markers that some expression has the wrong
> type, the error is silently ignored (or perhaps with an info marker), as
> if it was rewritten/wrapped to provide a value of the proper type.
>
> This is particularly convenient for functions that take functions as
> arguments. E.g. a Class could be coerced to Predicate<Object> to test
> for instanceof or a String could be coerced to Predicate<String> for
> regexp matching.
>
> Hallvard
Re: [xbase] value coercion [message #714352 is a reply to message #714299] Wed, 10 August 2011 11:51 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

Sebastian,

Looks similar to what I described. It would need to work for general
expressions (compare expected to actual type), not just literals. Where
is the conversion actually implemented, e.g. when/where/how is the code
for converting the string to number generated/injected?

Hallvard

On 10.08.11 10.59, Sebastian Zarnekow wrote:
>
> there is the SynonymTypeProvider which allows to define conversions
> statically. That is, int is synonym to Integer or Object[] is synonym to
> List<Object>. Xtext 2.1 will likely provide such a feature on the type
> reference level (XSynonymTypeReference). This would allow to do things
> like (pseudocode):
>
> _type(XStringLiteral literal, ..) {
> String value = literal.value;
> if (value.length == 1) {
> if (value contains only digits) {
> return synonymtype(typeof(String), typeof(Number), typeof(char));
> } else {
> return synonymtype(typeof(String), typeof(char));
> }
> } else if (value contains only digits) {
> return synonymtype(typeof(String), typeof(Number))
> } else {
> return typeof(String);
> }
> }
>
> Regards,
> Sebastian
Re: [xbase] value coercion [message #714441 is a reply to message #714352] Wed, 10 August 2011 13:58 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Hallvard,

this cannot work for arbitrary expressions since you (generally) do not
statically know the value of the string if it's not a literal.

This would be the implementation in the ITypeProvider.

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

On 10.08.11 13:51, Hallvard Trætteberg wrote:
> Sebastian,
>
> Looks similar to what I described. It would need to work for general
> expressions (compare expected to actual type), not just literals. Where
> is the conversion actually implemented, e.g. when/where/how is the code
> for converting the string to number generated/injected?
>
> Hallvard
>
> On 10.08.11 10.59, Sebastian Zarnekow wrote:
>>
>> there is the SynonymTypeProvider which allows to define conversions
>> statically. That is, int is synonym to Integer or Object[] is synonym to
>> List<Object>. Xtext 2.1 will likely provide such a feature on the type
>> reference level (XSynonymTypeReference). This would allow to do things
>> like (pseudocode):
>>
>> _type(XStringLiteral literal, ..) {
>> String value = literal.value;
>> if (value.length == 1) {
>> if (value contains only digits) {
>> return synonymtype(typeof(String), typeof(Number), typeof(char));
>> } else {
>> return synonymtype(typeof(String), typeof(char));
>> }
>> } else if (value contains only digits) {
>> return synonymtype(typeof(String), typeof(Number))
>> } else {
>> return typeof(String);
>> }
>> }
>>
>> Regards,
>> Sebastian
>
Previous Topic:The following token definitions are unreachable: RULE_ID
Next Topic:Enumeration type between ""
Goto Forum:
  


Current Time: Fri Apr 19 13:20:05 GMT 2024

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

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

Back to the top