Case Classes - Scala Documentation


#1

Case classes are like regular classes with a few key differences which we will go over. Case classes are good for modeling immutable data. In the next step of the tour, we’ll see how they are useful in pattern matching.


This is a companion discussion topic for the original entry at http://docs.scala-lang.org//tutorials/tour/case-classes.html

#2

The link at the top (second line) appears to be broken.


#3

@micseydel you mean the link to Pattern Matching?


#4

Yes, the href is http://docs.scala-lang.orgpattern-matching.html/


#5

I see that’s the case in this thread but when I go back to the original page, the link seems to work. Can you try it again from the original page? Perhaps try a hard refresh.


#6

The original page is fine, other than an extra slash - http://docs.scala-lang.org//tutorials/tour/pattern-matching.html

I’m not sure where you suggest a hard refresh. Does this page look alright after one? It still looks broken to me in an incognito window. I don’t think it’s a caching issue, either the link is wrong or there’s a bug in whatever you’re using to copy things.


#7

You can create a deep copy of an instance of a case class simply by using the copy method

Umm… Isn’t a case class copy() actually a shallow copy?

Given they are almost invariably immutable a deep copy would be an incredibly bad idea.


#8

Maybe a good idea to include in this page an example of what a case class corresponds to in terms of a regular class (toString(), equals(), copy(), …)?


#9

yes, good catch. fixed in https://github.com/scala/scala.github.com/commit/9cd1643f5797de4f5d091b11d1ec5c16cc74cc16


#10

But then it’s followed by “you can optionally change the constructor arguments”, which does use a deep copy, it may be worth clarifying that fact


#11

Not sure what you mean


#12

What I mean is that I don’t think it’s clear with the current wording that:

val myClone       = myCaseClass.copy()          // uses shallow copy
val myAlternative = myCaseClass.copy(foo = bar) // uses deep copy

#13

Both of those do shallow copies. “Deep” copying refers to recursively copying nested objects.


#14

Oh I see, my bad: I guess references don’t need to be cloned indeed