Reputation: 457
Is there any way to change a Kotlin when statement so that it includes an enum more efficiently?
For example:
val objectType = when (directoryType) {
UnixFileType.D -> "d"
UnixFileType.HYPHEN_MINUS -> "-"
UnixFileType.L -> "l"
}
To:
val objectType = when (directoryType.UnixFileType) {
D -> "d"
HYPHEN_MINUS -> "-"
L -> "l"
}
I have done some digging and haven't found a working solution. Does anybody know if I'm just messing something up or if it's a work in progress or just something they don't be adding?
Upvotes: 0
Views: 41
Reputation: 89548
You can do this, you just need to import the specific enum values directly:
import com.example.UnixFileType.D
import com.example.UnixFileType.HYPHEN_MINUS
import com.example.UnixFileType.L
val objectType = when (directoryType) {
D -> "d"
HYPHEN_MINUS -> "-"
L -> "l"
}
Or with a star import:
import com.example.UnixFileType.*
val objectType = when (directoryType) {
D -> "d"
HYPHEN_MINUS -> "-"
L -> "l"
}
Hint: You can convert the more verbose version by placing your cursor on one of the UnixFileType
's and choosing the Import members from com.example.UnixFileType
intention action.
Upvotes: 4