Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: is it possible to override default case class constructor?

Tags:

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

like image 664
adam77 Avatar asked Apr 18 '10 03:04

adam77


People also ask

Can we override default constructor?

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.

Can we extend case class in Scala?

Case classes can't be extended via subclassing. Or rather, the sub-class of a case class cannot be a case class itself.

Can case classes have methods Scala?

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.

What is the benefit of case class in Scala?

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.


2 Answers

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.

like image 35
Randall Schulz Avatar answered Sep 18 '22 11:09

Randall Schulz


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 vals) 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.

like image 157
Ken Bloom Avatar answered Sep 18 '22 11:09

Ken Bloom