I’m beginning Scala. Am I correct understanding that I should define a class as a case class if I’d like it’s arguments to be exposed as properties? Does not it introduce any side effects?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The boilerplate code generated for case classes carries a small but non-zero cost in bytecode. In addition to the
copymethod, there ishashCode,equalsandtoStringas well as the companion object factory method.More significant is the fact that it is inadvisable to derive classes from case classes. Deriving a case class from a case class really invites problems (and the compiler will yell at you). In particular, no overriding
copy(...)method is generated by the compiler, so you can get some odd failure modes if you try to copy a case class derived from a case class.If you keep your case classes at the leafs of any inheritance graphs, you’ll be fine.