Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Switching on two values in Groovy

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?

Name: Anonymous 2011-07-01 1:40

I don't know, but I know one thing:
Your code is PIG DISGUSTING

Name: Anonymous 2011-07-01 4:43

fuck

Name: Anonymous 2011-07-01 4:52

>>2
What code?

Name: Anonymous 2011-07-01 5:06

>>4
the one that's supposed to be between code tags

Don't change these.
Name: Email:
Entire Thread Thread List