Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xbase: type computation of switch's case expressions
Xbase: type computation of switch's case expressions [message #1552909] Thu, 08 January 2015 11:42 Go to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi

I noted that the XbaseTypeComputer computes the type of the case
expressions without type expectation from the switch expression; it just
does this

ITypeComputationState caseState = casePartState.withNonVoidExpectation();

Is that intentional?

This leads to this code in Xtend to be compiled fine

def sw(char c) {
switch(c) {
case '01' : println(0) // this should be a type mismatch
}
}

it is compiled to this Java code

public Integer sw(final char c) {
Integer _switchResult = null;
boolean _matched = false;
if (!_matched) {
if (Objects.equal(c, "01")) {
_matched=true;
_switchResult = InputOutput.<Integer>println(Integer.valueOf(0));
}
}
return _switchResult;
}

But shouldn't it raise a compilation error saying that it expects a char
instead of a String?

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Re: Xbase: type computation of switch's case expressions [message #1552995 is a reply to message #1552909] Thu, 08 January 2015 12:40 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Lorenzo,

Xtend lacks some validation for equality comparison of instances of
provable distinct types. This would detect the bogus case in your snippet.

Generally speaking it is valid to do something along these lines:

def Object someObject() {
return 'string';
}
def String someString() {
return 'string'
}

switch someString() {
case someObject(): println('')
}

Here you see that the expectation would have produced a type mismatch
even though this code is fine at runtime.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Xbase: type computation of switch's case expressions [message #1553189 is a reply to message #1552995] Thu, 08 January 2015 14:57 Go to previous message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 08/01/2015 13:40, Sebastian Zarnekow wrote:
> Hi Lorenzo,
>
> Xtend lacks some validation for equality comparison of instances of
> provable distinct types. This would detect the bogus case in your snippet.
>
> Generally speaking it is valid to do something along these lines:
>
> def Object someObject() {
> return 'string';
> }
> def String someString() {
> return 'string'
> }
>
> switch someString() {
> case someObject(): println('')
> }
>
> Here you see that the expectation would have produced a type mismatch
> even though this code is fine at runtime.

Hi Sebastian

OK, I see; indeed Xbase switch statement is much more powerful than
Java's one.

In my case (yes, still the stricter Xbase/Java version I'm working on),
I just had to add an additional check in the validator and I get my
desired type mismatch back :)

@Check
def void checkSwitch(XSwitchExpression sw) {
val switchExpressionType = getActualType(sw.^switch)
for (c : sw.cases) {
val caseType = getActualType(c.^case)
if (!switchExpressionType.isAssignableFrom(caseType)) {
error(
String.format("Type mismatch: cannot convert from %s to %s",
caseType.humanReadableName, switchExpressionType.humanReadableName
),
c,
XbasePackage.eINSTANCE.XCasePart_Case,
IssueCodes.INCOMPATIBLE_TYPES
)
}
}
}

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book


Previous Topic:keywords and terminals
Next Topic:Scopes.getElements(qualifiedName) can't resolve parentScope
Goto Forum:
  


Current Time: Fri Apr 26 04:52:54 GMT 2024

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

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

Back to the top