Skip to content

Commit b4e6fc1

Browse files
flomebulbishabosha
andauthored
Add code tabs for _tour/case-classes (#2533)
* Add code tabs for _tour/case-classes * Update _tour/case-classes.md Co-authored-by: Jamie Thompson <[email protected]> * Update _tour/case-classes.md Co-authored-by: Jamie Thompson <[email protected]> * Update case-classes.md Co-authored-by: Jamie Thompson <[email protected]>
1 parent 1f7cff1 commit b4e6fc1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

_tour/case-classes.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,64 @@ Case classes are like regular classes with a few key differences which we will g
1515

1616
## Defining a case class
1717
A minimal case class requires the keywords `case class`, an identifier, and a parameter list (which may be empty):
18+
19+
{% tabs case-classe_Book %}
20+
21+
{% tab 'Scala 2 and 3' for=case-classe_Book %}
1822
```scala mdoc
1923
case class Book(isbn: String)
2024

2125
val frankenstein = Book("978-0486282114")
2226
```
27+
{% endtab %}
28+
29+
{% endtabs %}
30+
2331
Notice how the keyword `new` was not used to instantiate the `Book` case class. This is because case classes have an `apply` method by default which takes care of object construction.
2432

2533
When you create a case class with parameters, the parameters are public `val`s.
34+
35+
{% tabs case-classe_Message_define %}
36+
37+
{% tab 'Scala 2 and 3' for=case-classe_Message_define %}
2638
```
2739
case class Message(sender: String, recipient: String, body: String)
2840
val message1 = Message("[email protected]", "[email protected]", "Ça va ?")
2941
3042
println(message1.sender) // prints [email protected]
3143
message1.sender = "[email protected]" // this line does not compile
3244
```
45+
{% endtab %}
46+
47+
{% endtabs %}
48+
3349
You can't reassign `message1.sender` because it is a `val` (i.e. immutable). It is possible to use `var`s in case classes but this is discouraged.
3450

3551
## Comparison
3652
Instances of case classes are compared by structure and not by reference:
53+
54+
{% tabs case-classe_Message_compare %}
55+
56+
{% tab 'Scala 2 and 3' for=case-classe_Message_compare %}
3757
```scala mdoc
3858
case class Message(sender: String, recipient: String, body: String)
3959

4060
val message2 = Message("[email protected]", "[email protected]", "Com va?")
4161
val message3 = Message("[email protected]", "[email protected]", "Com va?")
4262
val messagesAreTheSame = message2 == message3 // true
4363
```
64+
{% endtab %}
65+
66+
{% endtabs %}
67+
4468
Even though `message2` and `message3` refer to different objects, the value of each object is equal.
4569

4670
## Copying
4771
You can create a (shallow) copy of an instance of a case class simply by using the `copy` method. You can optionally change the constructor arguments.
72+
73+
{% tabs case-classe_Message_copy %}
74+
75+
{% tab 'Scala 2 and 3' for=case-classe_Message_copy %}
4876
```scala mdoc:nest
4977
case class Message(sender: String, recipient: String, body: String)
5078
val message4 = Message("[email protected]", "[email protected]", "Me zo o komz gant ma amezeg")
@@ -53,6 +81,10 @@ message5.sender // [email protected]
5381
message5.recipient // [email protected]
5482
message5.body // "Me zo o komz gant ma amezeg"
5583
```
84+
{% endtab %}
85+
86+
{% endtabs %}
87+
5688
The recipient of `message4` is used as the sender of `message5` but the `body` of `message4` was copied directly.
5789

5890
## More resources

0 commit comments

Comments
 (0)