title: Effective Enums in Kotlin with reverse lookup?
body: <p>I'm trying to find the best way to do a 'reverse lookup' on an enum in Kotlin. One of my takeaways from Effective Java was that you introduce a static map inside the enum to handle the reverse lookup. Porting this over to Kotlin with a simple enum leads me to code that looks like this:</p>
  
  <pre><code>enum class Type(val value: Int) {
      A(1),
      B(2),
      C(3);
  
      companion object {
          val map: MutableMap&lt;Int, Type&gt; = HashMap()
  
          init {
              for (i in Type.values()) {
                  map[i.value] = i
              } 
          }
  
          fun fromInt(type: Int?): Type? {
              return map[type]
          }
      }
  }
  </code></pre>
  
  <p>My question is, is this the best way to do this, or is there a better way? What if I have several enums that follow a similar pattern? Is there a way in Kotlin to make this code more re-usable across enums?</p>