Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it correct to define all classes as cases in Scala just to have all their arguments made properties automatically?

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?

like image 462
Ivan Avatar asked Aug 31 '10 03:08

Ivan


People also ask

How do you define a class in Scala?

Defining a classThe keyword new is used to create an instance of the class. We call the class like a function, as User() , to create an instance of the class.

For which kind of data should you use a case class in Scala?

A Scala Case Class is like a regular class, except it is good for modeling immutable data. It also serves useful in pattern matching, such a class has a default apply() method which handles object construction. A scala case class also has all vals, which means they are immutable.

What is difference between class and case class in Scala?

A class can extend another class, whereas a case class can not extend another case class (because it would not be possible to correctly implement their equality).

What is the benefit of case class in Scala?

The one of the topmost benefit of Case Class is that Scala Compiler affix a method with the name of the class having identical number of parameters as defined in the class definition, because of that you can create objects of the Case Class even in the absence of the keyword new.


2 Answers

The boilerplate code generated for case classes carries a small but non-zero cost in bytecode. In addition to the copy method, there is hashCode, equals and toString as 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.

like image 184
Randall Schulz Avatar answered Oct 03 '22 20:10

Randall Schulz


You will get properites for any parameters defined and they will be vals (i.e., finals)

case class User(name: String, group: String)
val user = User("jsmith", "admins")

// access both properties
println("name: %s group: %s".format(user.name, user.group))

You can get this behavior with regular (i.e., non-case classes) as well:

// creates two final public properties as well
class User(val name: String, val group: String)

// creates read/write public properties
class User(var name: String, var group: String)
val user = new User("jsmith", "admins")
user.group = "guests"

Case classes also bring a lot of other things as well such as a useful implementations of equality, hashcode and toString and a companion object with a factory method that eliminates the need to use new among other things.

As you can see, a case class is not required to achieve what you want but it gets you there quickly. As for side effects, defining a case class generates some bit of code behind the scenes to give you what was described in the previous paragraph. These are often useful and I tend not to worry about them but it is good to know about them.

like image 21
denis phillips Avatar answered Oct 03 '22 21:10

denis phillips