title: Scala case class: how to set computed value for a member
body: <p>I want to generate a random number to store in the "id" member of case class and be used a primary key when stored in the DB. If passed a non-zero value of id, then that value should be used, not generate new one.</p>
  
  <pre><code>case class User(name: String, id: Long = 0L)
  </code></pre>
  
  <p>I tried</p>
  
  <pre><code>object User {
    def apply(name: String, id: Long): User = 
      User(name, (id == 0L) ? &lt;some random number&gt; : id)
  }
  </code></pre>
  
  <p>But get some error like:</p>
  
  <pre><code>ambiguous reference to overloaded definition
  ...
  method apply is defined twice
  </code></pre>
  
  <p>I don't want to introduce another member in the case class.</p>