C#-like case when in kotlin?

Viewed 31

I come from a C# background and one really nice thing they offer is a way to add conditionals to a switch statement

switch(type)
{
    case "edit":
        // do something...
        break;
    case "delete" when user.isAdmin:
        // do something...
        break;
}

I'm curious if Kotlin has such an ability with their equivalent when? I'm not seeing that it's possible but I'm also still getting the hang of Kotlin so maybe there is a way.

1 Answers

I figured it out, guess I should have just asked Android studio XD

for those wondering, heres how:

when {
    type == "edit" -> {
        // do sometihng...
    }
    type == "delete" && user.isAdmin -> {
        // do sometihng...
    }
}

hope this helped someone else.

Related