Skip to content

Commit 9baf7e6

Browse files
authored
In response to Issue #1677, the first two sections were in the wrong order, so I reversed them. I also simplified some other text. (#1682)
1 parent 1919b6a commit 9baf7e6

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

_overviews/scala-book/case-classes.md

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
type: section
32
layout: multipage-overview
43
title: Case Classes
54
description: This lesson provides an introduction to 'case classes' in Scala.
@@ -26,6 +25,23 @@ These features are all demonstrated in the following sections.
2625

2726

2827

28+
## With `apply` you don’t need `new`
29+
30+
When you define a class as a `case` class, you don’t have to use the `new` keyword to create a new instance:
31+
32+
```scala
33+
scala> case class Person(name: String, relation: String)
34+
defined class Person
35+
36+
// "new" not needed before Person
37+
scala> val christina = Person("Christina", "niece")
38+
christina: Person = Person(Christina,niece)
39+
```
40+
41+
As discussed in the previous lesson, this works because a method named `apply` is generated inside `Person`’s companion object.
42+
43+
44+
2945
## No mutator methods
3046

3147
Case class constructor parameters are `val` fields by default, so an *accessor* method is generated for each parameter:
@@ -35,7 +51,7 @@ scala> christina.name
3551
res0: String = Christina
3652
```
3753

38-
But, mutator methods are not generated:
54+
But, *mutator* methods are not generated:
3955

4056
```scala
4157
// can't mutate the `name` field
@@ -49,26 +65,9 @@ Because in FP you never mutate data structures, it makes sense that constructor
4965

5066

5167

52-
## An `apply` method means you don’t need `new`
53-
54-
When you define a class as a `case` class, you don’t have to use the `new` keyword to create a new instance:
55-
56-
```scala
57-
scala> case class Person(name: String, relation: String)
58-
defined class Person
59-
60-
// "new" not needed before Person
61-
scala> val christina = Person("Christina", "niece")
62-
christina: Person = Person(Christina,niece)
63-
```
64-
65-
As discussed in the previous lesson, this works because a method named `apply` is generated inside `Person`’s companion object.
66-
67-
68-
6968
## An `unapply` method
7069

71-
In the previous lesson on companion objects you saw how to write `unapply` methods. A great thing about case classes is that they automatically generate an `unapply` method, so you don’t have to write one.
70+
In the previous lesson on companion objects you saw how to write `unapply` methods. A great thing about a case class is that it automatically generates an `unapply` method for your class, so you don’t have to write one.
7271

7372
To demonstrate this, imagine that you have this trait:
7473

@@ -85,7 +84,7 @@ case class Student(name: String, year: Int) extends Person
8584
case class Teacher(name: String, specialty: String) extends Person
8685
```
8786

88-
Because those are defined as case classes — and a case class has a built-in `unapply` method — you can write a match expression like this:
87+
Because those are defined as case classes — and they have built-in `unapply` methods — you can write a match expression like this:
8988

9089
```scala
9190
def getPrintableString(p: Person): String = p match {
@@ -187,11 +186,3 @@ res0: Person = Person(Christina,niece)
187186
While all of these features are great benefits to functional programming, as they write in the book, [Programming in Scala](https://www.amazon.com/Programming-Scala-Updated-2-12/dp/0981531687/) (Odersky, Spoon, and Venners), “the biggest advantage of case classes is that they support pattern matching.” Pattern matching is a major feature of FP languages, and Scala’s case classes provide a simple way to implement pattern matching in match expressions and other areas.
188187

189188

190-
191-
192-
193-
194-
195-
196-
197-

0 commit comments

Comments
 (0)