title: Kotlin initialization: how to fail
body: <p>I need to parse Firebase <code>DataSnapshot</code> (a JSON object) into a data class, whose properties include enum and list. So I prefer parsing it manually by passing the <code>DataSnapshot</code> into a secondary constructor. However, I want the construction to fail if some fields are missing in the <code>DataSnapshot</code>. In Swift, it has failable initializer that returns null so I know it has failed. How to do that in Kotlin?</p>
  
  <p>The following is my attempt, but I assume you can't just return from a constructor, right?</p>
  
  <pre><code>data class Unit(val name: String, val type: UnitType, val components: List&lt;Component&gt;) {
      constructor(snapshot: DataSnapshot) {
          name = snapshot.child("name").value as? String ?: return
          val typeString = snapshot.child("type").value as? String ?: return
          type = UnitType.values().firstOrNull { it.abbrv == typeString } ?: return
          ...
      }
  
  }
  </code></pre>