title: How to initialize one field using another in case class?
body: <p>Suppose I have a case class like</p>
  
  <pre><code>case class Person(fname:String, lname:String, nickName:Option[String] = None)
  </code></pre>
  
  <p>On creating an instance like Person("John", "Doe"), I want nickName to be automatically assigned to fname, if one is not given. Eg:</p>
  
  <pre><code>val p1 = Person("John", "Doe")
  p1.nickName.get == "John"
  
  val p2 = Person("Jane", "Doe", "Joe")
  p2.nickName.get == "Joe"
  </code></pre>
  
  <p>How can auto assignment of one field from another field be achieved?</p>
  
  <p>Trying the solutions below in repl. I think this is something to do with repl</p>
  
  <pre><code>scala&gt; case class Person(fname: String, lname:String, nickName:Option[String])
  defined class Person
  
  scala&gt; object Person { def apply(fname:String, lname:String):Person = {Person(fname, lname, Some(fname))}}
    console:9: error: too many arguments for method apply: (fname: String, lname: String)Person in object Person
     object Person { def apply(fname:String, lname:String):Person = {Person(fname, lname, Some(fname))}}
  </code></pre>