Andrey Hihlovskiy
Professional blog on groovy, gradle, Java, Javascript and other stuff.
Tag Archives: type
groovy switch: nasty bug
September 20, 2013
Posted by on I found nasty error in Groovy compiler. Consider the following code:
byte b = 1 switch(b) { case 0..9: println 'it is between 0 and 9' break default: println 'it is something else' }
It executes ‘default’ part, not the part with 0..9, which is not what a programmer would typically expect.
The reason behind it should be related to type conversion between “byte” and “int” types. With the following workaround:
switch((int)b)
the program executes “proper” case.
Recent Comments