title: kotlin safe conversion from string to enum
body: <p>I need to convert strings to Enum values, but want a function which returns null if the string is not an enum.</p>
  
  <pre><code>enum class Colors{
     Red, Green, Blue
  
  } 
  </code></pre>
  
  <p>I can used Colors.valueOf(testString) provided testString is value, but there will be an exception if it is not valid, and I want a null in that case.</p>
  
  <p>Because I want to this often, an extension function would be ideal.  But the extension needs to operate on the class Colors, and not an object of type Colors.</p>
  
  <p>Anyone know how to write such an extension? Ideally one that is generic for any enum class.</p>
  
  <p>It is simple to write a top level function, but I am seeking one that acts as the  standard 'method' does</p>
  
  <pre><code>// instead of 
  val willGetAnException = Colors.valueOf("Yellow") // standard existing fun
  val willGetNull = Colors.valueOrNullOf("Orange")  // new fun i seek
  </code></pre>
  
  <p>And ideally one that is generic and works for any enum </p>