Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Case Classes?

 
Teena George
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am learning Scala. I am kind of stuck with the concept of Case Classes. Could somebody please explain me this in simple terms?
Do we have something similar in Java? I ask because I am a Java programmer and it would be easier to understand this if we have something similar in Java.

Thanks in advance,
Teena
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no real Java analog to case classes. Case classes in Scala are regular classes with some sytactic sugar added. Specifically, the compiler a toString method, and it generates a companion object with 'special' apply and unapply methods. The key thing to remember with case classes is that you don't have to use the keyword 'new' to create an instance (because of the compiler generated apply method), and they can be used out of the box in pattern matches (because of the compiler generated unapply method).
 
Teena George
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Garret. I got your point.

Regards,
Teena
 
Teena George
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is Case Class very commonly used?
Where can I study scala pattern matching in detail? I am going through the details of scala-lang.org site and the pdf files (Scala tutorial) from the site. Is there any simpler and elaborative explanation available? I am kind of weak in pattern matching.

Thanks,
Teena
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Case classes are quite common in Scala, as is pattern matching. The most common cases are pattern matching on Options or Lists. Using a case class for pattern matching in your own class heirarchy is realtively straightforward.

 
Teena George
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Garrett. I will try it out.

Regards,
Teena
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say there is a relation to Enumerations in Java, but just a relation.
 
Teena George
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan.
 
Inca Nauthiz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't recommend overusing case classes. Scala Tips mentioned inheritance ambiguities with case classes.
In my little experience case classes help in small matters, in such points, where no extensions are planned (for example, in domain model parts, to avoid equals/hashCode boilderplates). They greatly increase readability in such cases. On the other hand, using them just to save three characters from unnecessary "new" keyword may introduce other concerns. Use them wisely.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic