|
|
Re: Xbase: type computation of switch's case expressions [message #1553189 is a reply to message #1552995] |
Thu, 08 January 2015 14:57 |
|
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
HOME: http://www.lorenzobettini.it
TDD Book: https://leanpub.com/tdd-buildautomation-ci
Xtext Book: https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition
|
|
|
Powered by
FUDForum. Page generated in 0.03481 seconds