title: Create instance in Kotlin and only set property if not null
body: <p>I have a data class that looks like this:</p>
  
  <pre><code>data class MyDataCass(val x: String, val y: String, 
                        val something: Something = Something(),
                        val somethingElse : SomethingElse = SomethingElse())
  </code></pre>
  
  <p>Since this class is going to be used from both Kotlin and Java I've also created a builder to make it easier to construct it from Java. The problem is that <code>something</code> and <code>somethingElse</code> are optional and I'd like to avoid duplication of the default values. This is essentially what I've tried:</p>
  
  <pre><code>data class MyDataCass(val x: String, val y: String, 
                        val something: Something = Something(), 
                        val somethingElse : SomethingElse = SomethingElse()) {
  
      class Builder() {
          private lateinit var x: String
          private lateinit var y: String
          private var something: Something? = null
          private var somethingElse: SomethingElse? = null
  
          // builder functions to set the properties defined above
          ...
  
          fun build() = MyDataCass(x, y, something = ??, somethingElse = ??)
      }
  }
  </code></pre>
  
  <p>What I'd like to do is to set <code>something</code> and <code>somethingElse</code> <em>only if they've been defined by the builder</em> (i.e. they are not <code>null</code>), otherwise I'd like to use the default values as defined by the data class constructor. I could of course change the build function to look like this:</p>
  
  <pre><code>fun build() = if (something == null &amp;&amp; somethingElse == null) {
          MyDataCass(x, y)
      } else if(somethingElse == null) {
          MyDataCass(x, y, something = something)
      } else {
          MyDataClass(x,y, somethingElse = somethingElse)
      }
  }
  </code></pre>
  
  <p>but I find this to be quite verbose and error prone. Is there a less verbose or more idiomatic way of achieving this?</p>