10000 Reworked case class tutorial #547 by jatcwang · Pull Request #599 · scala/docs.scala-lang · GitHub
[go: up one dir, main page]

Skip to content

Reworked case class tutorial #547 #599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 18, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Put tut:fail for code block that should fail to compile
  • Loading branch information
jatcwang committed Oct 10, 2016
commit 2dfa9c46d85919df3951c3c86c6ff86463426230
9 changes: 6 additions & 3 deletions tutorials/tour/case-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ val title = emailFromJohn.title
println(title) // prints "Greetings From John!"
```

With case classes, you cannot mutate their fields directly. Instead, you make a copy using the `copy` method.
As seen below, you can replace just some of the fields:
With case classes, you cannot mutate their fields directly.

```tut
```tut:fail
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.
```

Instead, you make a copy using the `copy` method. As seen below, you can replace just some of the fields:

```tut
val editedEmail = emailFromJohn.copy(title = "I am learning Scala!", body = "It's so cool!")

println(emailFromJohn) // prints "Email(john.doe@mail.com,Greetings From John!,Hello World!)"
Expand Down
0