The Kotlin idiom for Java's Enum.valueof("foo")


#1

If I have a Kotlin enum

enum class Opcode {   unsubscribe }

and I have a String s == “unsubscribe”, how do I get an Opcode given this string?  In Java I would do

Opcode o = Opcode.valueOf(s)


#2

I've come up with this, but I was hoping for something more compact:

           if( args[t].equals("--opcode")) {            val oc = args[++t]            when (oc) {                    "unsubscribe" -> opcode = Opcode.unsubscribe                    else -> {                    throw  RuntimeException("invalide opcode ${oc}")                    }            }            }


#3

Unfortunately, this syntax is not supported yet. I have created request in our issue tracker: KT-1517


#4

Thanks!