Kotlin enum with string values
An enum class with string values.
enum class ActionType(val type: String) { BLOCKING("blocking"), BACKGROUND("background"), CALLBACKS("callbacks"), SUSPEND("suspend"), CONCURRENT("concurrent"), NOT_CANCELABLE("not cancellable"), PROGRESS("progress"), CHANNELS("channels"), CANCEL("cancel"), }
To get the string value from this enum
fun getStringVal(actionType: ActionType): String { return actionType.type; }
To get the enum value
fun printEnumVal(actionType: ActionType) { print(actionType) } printEnumVal(ActionType.SUSPEND); // prints SUSPEND
Switch statements with kotlin enum
fun onAction(actionType: ActionType) { when (actionType) { ActionType.BLOCKING -> { print(actionType.toString()) // prints BLOCKING print(actionType) // prints BLOCKING print(actionType.type) // prints blocking } ActionType.BACKGROUND -> { print(actionType) // prints BACKGROUND print(actionType.type) // prints background } ActionType.CALLBACKS -> { print(actionType) // prints CALLBACKS print(actionType.type) // prints callbacks } ActionType.SUSPEND -> { print(actionType) // prints SUSPEND print(actionType.type) // prints suspend } ActionType.CONCURRENT -> { print(actionType) // prints CONCURRENT print(actionType.type) // prints concurrent } ActionType.NOT_CANCELABLE -> { print(actionType) // prints NOT_CANCELABLE print(actionType.type) // prints not cancellable } ActionType.PROGRESS -> { print(actionType) // prints PROGRESS print(actionType.type) // prints progress } ActionType.CHANNELS -> { print(actionType) // prints CHANNELS print(actionType.type) // prints channels } ActionType.CANCEL -> { print(actionType) // prints CANCEL print(actionType.type) // prints cancel } else -> { print("Not a valid action type") } } }
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts