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

**URL:** <https://discuss.kotlinlang.org/t/the-kotlin-idiom-for-javas-enum-valueof-foo/1279>\
**Category:** Uncategorized\
**Created:** [March 6, 2012, 4:09pm UTC](https://discuss.kotlinlang.org/t/the-kotlin-idiom-for-javas-enum-valueof-foo/1279 "2012-03-06T16:09:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ae6rt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ae6rt/32/1591_2.png) [@ae6rt](https://discuss.kotlinlang.org/u/ae6rt)\
**Post date:** [March 6, 2012, 4:09pm UTC](https://discuss.kotlinlang.org/t/the-kotlin-idiom-for-javas-enum-valueof-foo/1279/1 "2012-03-06T16:09:00Z")

</div>

If I have a Kotlin enum

enum class Opcode { &nbsp;&nbsp;unsubscribe }

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

Opcode o = Opcode.valueOf(s)

---

<div class="post-metadata">

**Author:** ![ae6rt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ae6rt/32/1591_2.png) [@ae6rt](https://discuss.kotlinlang.org/u/ae6rt)\
**Post date:** [March 6, 2012, 5:01pm UTC](https://discuss.kotlinlang.org/t/the-kotlin-idiom-for-javas-enum-valueof-foo/1279/2 "2012-03-06T17:01:45Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![geevee](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/geevee/32/1475_2.png) [@geevee](https://discuss.kotlinlang.org/u/geevee)\
**Post date:** [March 6, 2012, 8:31pm UTC](https://discuss.kotlinlang.org/t/the-kotlin-idiom-for-javas-enum-valueof-foo/1279/3 "2012-03-06T20:31:21Z")

</div>

Unfortunately, this syntax is not supported yet. I have created request in our issue tracker: [KT-1517](http://youtrack.jetbrains.com/issue/KT-1517)

---

<div class="post-metadata">

**Author:** ![ae6rt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ae6rt/32/1591_2.png) [@ae6rt](https://discuss.kotlinlang.org/u/ae6rt)\
**Post date:** [March 6, 2012, 10:10pm UTC](https://discuss.kotlinlang.org/t/the-kotlin-idiom-for-javas-enum-valueof-foo/1279/4 "2012-03-06T22:10:03Z")

</div>

Thanks!
