title: sealed class vs enum when using associated type
body: <p>I'd like to create a color object based on an <code>Int</code>. I can achieve the same result using <code>sealed class</code> and <code>enum</code> and was wondering if one is better than the other.</p>
  
  <p>Using <code>sealed class</code>:</p>
  
  <pre><code>sealed class SealedColor(val value: Int) {
      class Red : SealedColor(0)
      class Green : SealedColor(1)
      class Blue : SealedColor(2)
  
      companion object {
          val map = hashMapOf(
              0 to Red(),
              1 to Green(),
              2 to Blue()
          )
      }
  }
  
  val sealedColor: SealedColor = SealedColor.map[0]!!
  when (sealedColor) {
                  is SealedColor.Red -&gt; print("Red value ${sealedColor.value}")
                  is SealedColor.Green -&gt; print("Green value ${sealedColor.value}")
                  is SealedColor.Blue -&gt; print("Blue value ${sealedColor.value}")
              }
  </code></pre>
  
  <p>Using <code>enum</code>:</p>
  
  <pre><code>enum class EnumColor(val value: Int) {
      Red(0),
      Green(1),
      Blue(2);
  
      companion object {
          fun valueOf(value: Int): EnumColor {
              return EnumColor
                  .values()
                  .firstOrNull { it.value == value }
                      ?: throw NotFoundException("Could not find EnumColor with value: $value")
          }
      }
  }
  
  val enumColor: EnumColor = EnumColor.valueOf(0)
  when (enumColor) {
              EnumColor.Red -&gt; print("Red value ${enumColor.value}")
              EnumColor.Green -&gt; print("Green value ${enumColor.value}")
              EnumColor.Blue -&gt; print("Blue value ${enumColor.value}")
          }
  </code></pre>
  
  <p>Are they equivalent in term of performance? Is there a better kotlin way to achieve the same result?</p>