[xbase] value coercion [message #714272] |
Wed, 10 August 2011 08:01 |
Eclipse User |
|
|
|
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 |
Sebastian Zarnekow 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 |
Eclipse User |
|
|
|
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 |
Sebastian Zarnekow 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
>
|
|
|
Powered by
FUDForum. Page generated in 0.03886 seconds