https://kotlinlang.org logo
z

zain

08/01/2021, 10:40 AM
How to do multi switch cases like Swift in Kotlin using when?
Copy code
func getMode() -> Mode {
        var mode: Mode = .live
        switch (gdIsLocked, gdIsCurrent, matchIsLocked, matchIsCurrent) {
        case (0,1,0,1):
            mode = .manage
        case (1,1,1,1):
            mode = .live
        case (1,1,1,0):
            mode = .substitution
        case (1,2,1,2):
            mode = .points
        case (0,0,0,0):
            mode = .upcoming
        default:
            mode = .upcoming
        }
        return mode
    }

    enum Mode {
        case manage
        case substitution
        case live
        case points
        case upcoming
    }
❤️ 1
Has 4 values and then checked in the case is it possible to do in Kotlin?
Copy code
switch (gdIsLocked, gdIsCurrent, matchIsLocked, matchIsCurrent)
n

Nir

08/01/2021, 12:59 PM
I think you'd need to create a dataclass holding 4 booleans
e

edrd

08/01/2021, 1:32 PM
getMode.kt
☝️ Unless I understood it wrong, it should be possible using lists
z

zain

08/01/2021, 2:52 PM
Need to test this @edrd
n

Nir

08/01/2021, 3:48 PM
Yeah a list would work fine as well
4 Views