8000 Simplify case class section · soronpo/scala.github.com@c14cab8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c14cab8

Browse files
committed
Simplify case class section
1 parent ccc0c27 commit c14cab8

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

tutorials/tour/basics.md

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,7 @@ We will cover classes in depth [later](classes.md).
152152

153153
## Case Classes
154154

155-
By default, classes are compared by reference. If you want to compare them by values, you need to override `equals` and `hashCode` methods.
156-
157-
```
158-
class Point(val x: Int, val y: Int) {
159-
override def equals(o: Any) = o match {
160-
case that: Point => x == that.x && y == that.y
161-
case _ => false
162-
}
163-
override def hashCode = (x.hashCode << 16) + y.hashCode
164-
}
165-
```
166-
167-
While it works, it's tedious to define those methods for all classes you want to compare by value.
168-
169-
Thankfully, Scala has another type of class called `case class` that's compared by value by default.
155+
Scala has a special type of class called case class that's immutable by default and compared by value. You can define case classes with the `case class` keywo 10000 rd.
170156

171157
```
172158
case class Point(x: Int, y: Int)
@@ -175,28 +161,26 @@ case class Point(x: Int, y: Int)
175161
You can instantiate case classes without `new` keyword.
176162

177163
```
178-
val point = Point(1, 2) // no "new" here
164+
val point = Point(1, 2)
179165
val anotherPoint = Point(1, 2)
180166
val yetAnotherPoint = Point(2, 2)
181-
println(point == anotherPoint) // true
182-
println(point == yetAnotherPoint) // false
183-
```
184-
185-
Case classes are immutable by default, but it also provides `copy` method so that you can easily create another instance of the class while reusing the values from exiting instances.
186167
187-
Using `copy` method, you can also write the above code like below.
168+
if (point == anotherPoint) {
169+
println(point + " and " + anotherPoint + " are the same.")
170+
} else {
171+
println(point + " and " + anotherPoint + " are different.")
172+
}
173+
// Point(1,2) and Point(1,2) are the same.
188174
175+
if (point == yetAnotherPoint) {
176+
println(point + " and " + yetAnotherPoint + " are the same.")
177+
} else {
178+
println(point + " and " + yetAnotherPoint + " are different.")
179+
}
180+
// Point(1,2) and Point(2,2) are different.
189181
```
190-
val point = Point(1, 2)
191-
val anotherPoint = point.copy()
192-
val yetAnotherPoint = point.copy(x = 2)
193-
println(point == anotherPoint) // true
194-
println(point == yetAnotherPoint) // false
195-
```
196-
197-
Because of case classes, you almost never see classes overriding `equals` and `hashCode` methods in Scala.
198182

199-
There are many other features you get out-of-the-box by using case classes. We will cover them in depth [later](case-classes.md).
183+
There is a lot more to case classes that we'd like to introduce, and we are convinced you will fall in love with it! We will cover them in depth [later](case-classes.md).
200184

201185
## Singleton Objects
202186

0 commit comments

Comments
 (0)
0