Name: Anonymous 2011-06-30 20:05
Currently I'm experimenting with Groovy because I'm writing an MMORPG with Grails. It happened that I had to switch on two values. First I used the verbose approach:
switch (firstValue) {
case FOO:
switch (secondValue) {
case BAR:
return someResult
default:
return defaultResult
}
case BAZ:
switch (secondValue) {
case QUX:
return someOtherResult
default:
return defaultResult
}
default:
return defaultResult
}
Then I tried this
switch ([firstValue, secondValue]) {
case [FOO, BAR]:
return someResult
case [BAZ, QUX]:
return someOtherResult
default:
return defaultResult
}
But it always returns the defaultResult because if you use a list as a case, Groovy's switch checks if the value you switch on is contained in the list. So I ended up with this
switch ([firstValue, secondValue]) {
case [[FOO, BAR]]:
return someResult
case [[BAZ, QUX]]:
return someOtherResult
default:
return defaultResult
}
tl;dr Nobody actually knows what stuff you can do with Groovy. Any other approaches to switch on two values in Groovy?
switch (firstValue) {
case FOO:
switch (secondValue) {
case BAR:
return someResult
default:
return defaultResult
}
case BAZ:
switch (secondValue) {
case QUX:
return someOtherResult
default:
return defaultResult
}
default:
return defaultResult
}
Then I tried this
switch ([firstValue, secondValue]) {
case [FOO, BAR]:
return someResult
case [BAZ, QUX]:
return someOtherResult
default:
return defaultResult
}
But it always returns the defaultResult because if you use a list as a case, Groovy's switch checks if the value you switch on is contained in the list. So I ended up with this
switch ([firstValue, secondValue]) {
case [[FOO, BAR]]:
return someResult
case [[BAZ, QUX]]:
return someOtherResult
default:
return defaultResult
}
tl;dr Nobody actually knows what stuff you can do with Groovy. Any other approaches to switch on two values in Groovy?