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?
Scala case classes are syntactic sugar. When you create a case class the Scala compiler will create a companion object with an
apply
and anunapply
method for you, which you can then use as if it simply exists. Here is a link to more in depth information on case classes.Case classes are implicitly accompanied with a companion object with an
apply()
that has the same arguments as the primary constructor of the case class.That is:
Will be accompanied by
Now if you also explicitly define a companion object, you can think of it as appending to the implicitly defined one.
For instance, in your example you added one new
apply
to the companion object:This statement, you can think of it as if it is creating an combined companion object:
Now, if you decide to add your own version of the
apply
that has the same arguments as the primary constructor, then this will overshadow the default behavior of the case class.Now, if you call
CasePerson("hi")
it will instead generate:You may also give your
firstName
class parameter a default value. So you can create an instance with passing no parameter.Example: