https://kotlinlang.org logo
#announcements
Title
# announcements
k

kevin.cianfarini

10/07/2019, 6:17 PM
is it possible to write a function to get an enum from a value? I know in Java you would write a static function, but enums in kotlin can't have companion objects. Looking to get the enum associated with
token
here.
Copy code
internal enum class BencoderTokens(private val token: Byte) {
    INTEGER('i'.toByte()),
    STRING(':'.toByte()),
    LIST('l'.toByte()),
    DICT('d'.toByte()),
    END('e'.toByte())
}
s

streetsofboston

10/07/2019, 6:23 PM
??? Enums can have companion objects.
Copy code
internal enum class BencoderTokens(private val token: Byte) {
    INTEGER('i'.toByte()),
    STRING(':'.toByte()),
    LIST('l'.toByte()),
    DICT('d'.toByte()),
    END('e'.toByte());

    companion object {
        fun create(...) : BencoderTokens { .. }
    }
}
k

kevin.cianfarini

10/07/2019, 6:29 PM
welp, I'm dumb 🙃
thanks!