Just wondering if this is possible. What I would actually like to do is check and possibly modify one of the arguments before it is stored as a val.
Alternatively, I could use an overload and make the default constructor private. In which case I would also like to make private the default factory constructor in the companion object, how would I do that?
Many thanks.
Adam
edit: well i figured out that making the default constructor private also makes the default factory constructor private, so i have a solution, i'm still interested to know if the default constructor is overridable though
Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. But, a constructor cannot be overridden. If you try to write a super class's constructor in the sub class compiler treats it as a method and expects a return type and generates a compile time error.
Case classes can't be extended via subclassing. Or rather, the sub-class of a case class cannot be a case class itself.
Case Classes You can construct them without using new. case classes automatically have equality and nice toString methods based on the constructor arguments. case classes can have methods just like normal classes.
When we use the case keyword, the Scala compiler adds these methods for free to avoid boilerplate code. A companion object with apply and unapply methods, that's why there is no need to use new keywords to create instances of a Case classes copy method. Easy to use in pattern matching.
The presence of secondary case class constructors don't cause the compiler to produce additional factory methods in the class's companion, so you won't get the convenience of CaseClaseName(«secondary constructor parameter list»>)
for creating them. You'll have to use the new
keyword.
It's better to put the sort of logic you're describing in alternate factory methods in the companion object and stick to using the primary constructor.
You do not have the option of changing the way the default constructor stores its parameters (e.g. by modifying the parameters before they are stored as val
s) but you do have the option of throwing an exception if the parameters are wrong (this will occur after the parameters are stored)
case class Foo(x:Int){
if (x<0) throw SomeException;
}
You also have the option of implementing additional constructors that call the first constructor
case class Foo(x:Int){
def this(x:Int,y:Int) = this(x+y)
}
but those don't get factory methods.
You could easily create the factory method yourself by adding it to the companion object
object Foo{
def apply(x:Int,y:Int) = new Foo(x,y)
}
Anything else more complicated than that, and you have to forgo the case class and implement it's parts on your own: apply
, unapply
, equals
, and hashCode
. Programming in Scala talks about how to do all of these, giving good formulas for equals
and hashCode
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With