Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override enum toString() in Kotlin?

Tags:

android

kotlin

How to customize toString() method for enum in Kotlin?

enum class GuideType(type: String) {
    DEF_TYPE("default"),

    override fun toString(): String {
        return type // not working!
    }
}
like image 537
Sergey Shustikov Avatar asked Nov 25 '18 21:11

Sergey Shustikov


1 Answers

Default constructor params need to be either var or val to be accessible outside the init block. Also you need to add semicolor after last enum item to add any new functions or overrides.

enum class GuideType(var type: String) {
    DEF_TYPE("default");

    override fun toString(): String {
        return type // working!
    }
}
like image 133
iFanie Avatar answered Sep 28 '22 15:09

iFanie