Assuming we have the following case class:
case class CasePerson(firstName: String)
And we also define a companion object for it:
object CasePerson {
def apply() = new CasePerson( "XYZ" )
}
Notice that in the example above I explicitly defined a companion object with an apply
method, without defining the the default apply method:
// This "default" apply has the same argument as the primary constructor of the case class
def apply(firstName : String) = new CasePerson(firstName)
Q: So where does Scala gets this "default" apply? I explicitly defined the companion object here without the default apply and the compiler still knows how to execute this:
val casePerson = CasePerson("PQR")
How does this work?