Switch type guard should not be forced to be compatible with variable type

76 views
Skip to first unread message

Typhoon Storm

unread,
Apr 3, 2016, 8:15:04 AM4/3/16
to Xtend Programming Language
Hi,
    I found this annoying: Xtend forces type guard of case clause to be compatible with switch variable's type. Shown as follows:

def foo() {
val i = 1
switch i {
List: { // <--- Compile error: Cannot cast from int or Integer to List
}
}
}

    And in following case, it even prevents the source file from being saved to disk:

---------------------------------------------------------------------
package foo /* Multiple markers at this line
- UnsupportedOperationException: Missing implementation for int
   & List (see logs for details)
- Missing implementation for int & List - See error log for details
- Missing implementation for Formatter & List - See error log
   for details
*/

import java.util.List

class Formatter {
override toString() {
val int x = 1
switch x {
List: {
return '''[
«FOR e : x»e«ENDFOR»       // <---------------  this line causes errors at 1st line and makes the file unsaveable
]'''
}
}
return ""
}
}

    I checked errors/warnings page in xtend compiler settings and didn't find related option.
    In first case, it could be interpreted that the var i is Integer type and cannot be a List, so the case typeguarded by List can never be executed. However IMHO, I think this is a too strict limitation to disallow this code, since I know what I'm doing. It's just another kind of dead code, like: if (false) {..}, far from illegal. It would be helpful when writing initial, prototype or not-prefectly-understood code.
    So I suggest loosen the limitation, or at least to add a configure option to let programmer decide whether to allow or disallow this kind of code.

Sven Efftinge (sven@efftinge.de)

unread,
Apr 4, 2016, 8:36:12 AM4/4/16
to xtend...@googlegroups.com
Not being able to save a file is definitely not intended. Please file a bugzilla.

For the first snippet I don't understand why you would want to write that. An int can and will not be a list. 
Please note, that it is not only forbidden because it is wrong, but also because Java doesn't allow that cast either. So we would have to generate special code to make it work. 
IMHO it's better to write that kind of code (e.g. first cast to Object) yourself in Xtend so the next reader gets a sign that this section is a bit "special".

Sven

--
You received this message because you are subscribed to the Google Groups "Xtend Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xtend-lang+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Typhoon Storm

unread,
Apr 4, 2016, 9:21:16 AM4/4/16
to Xtend Programming Language

Typhoon Storm

unread,
Apr 4, 2016, 9:21:44 AM4/4/16
to Xtend Programming Language
A bugzilla is filed at https://bugs.eclipse.org/bugs/show_bug.cgi?id=491009

On Monday, April 4, 2016 at 8:36:12 PM UTC+8, Sven Efftinge wrote:
Reply all
Reply to author
Forward
0 new messages