You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/tour/case-classes.md
+6-3Lines changed: 6 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -41,12 +41,15 @@ val title = emailFromJohn.title
41
41
println(title) // prints "Greetings From John!"
42
42
```
43
43
44
-
With case classes, you cannot mutate their fields directly. Instead, you make a copy using the `copy` method.
45
-
As seen below, you can replace just some of the fields:
44
+
With case classes, you cannot mutate their fields directly.
46
45
47
-
```tut
46
+
```tut:fail
48
47
emailFromJohn.title = "Goodbye From John!" // This is a compilation error. We cannot assign another value to val fields, which all case classes fields are by default.
48
+
```
49
49
50
+
Instead, you make a copy using the `copy` method. As seen below, you can replace just some of the fields:
51
+
52
+
```tut
50
53
val editedEmail = emailFromJohn.copy(title = "I am learning Scala!", body = "It's so cool!")
51
54
52
55
println(emailFromJohn) // prints "Email(john.doe@mail.com,Greetings From John!,Hello World!)"
0 commit comments