-
Notifications
You must be signed in to change notification settings - Fork 1k
Add code tabs for _tour/case-classes #2533
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
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,36 +15,64 @@ Case classes are like regular classes with a few key differences which we will g | |
|
||
## Defining a case class | ||
A minimal case class requires the keywords `case class`, an identifier, and a parameter list (which may be empty): | ||
|
||
{% tabs case-classe_Book %} | ||
|
||
{% tab 'Scala 2 and 3' for=case-classe_Book %} | ||
```scala mdoc | ||
case class Book(isbn: String) | ||
|
||
val frankenstein = Book("978-0486282114") | ||
``` | ||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
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. | ||
|
||
When you create a case class with parameters, the parameters are public `val`s. | ||
|
||
{% tabs case-classe_Message_define %} | ||
|
||
{% tab 'Scala 2 and 3' for=case-Message_define %} | ||
``` | ||
case class Message(sender: String, recipient: String, body: String) | ||
val message1 = Message("[email protected]", "[email protected]", "Ça va ?") | ||
|
||
println(message1.sender) // prints [email protected] | ||
message1.sender = "[email protected]" // this line does not compile | ||
``` | ||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
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. | ||
|
||
## Comparison | ||
Instances of case classes are compared by structure and not by reference: | ||
|
||
{% tabs case-classe_Message_compare %} | ||
|
||
{% tab 'Scala 2 and 3' for=case-Message_compare %} | ||
flomebul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```scala mdoc | ||
case class Message(sender: String, recipient: String, body: String) | ||
|
||
val message2 = Message("[email protected]", "[email protected]", "Com va?") | ||
val message3 = Message("[email protected]", "[email protected]", "Com va?") | ||
val messagesAreTheSame = message2 == message3 // true | ||
``` | ||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
Even though `message2` and `message3` refer to different objects, the value of each object is equal. | ||
|
||
## Copying | ||
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. | ||
|
||
{% tabs case-classe_Message_copy %} | ||
|
||
{% tab 'Scala 2 and 3' for=case-Message_copy %} | ||
flomebul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```scala mdoc:nest | ||
case class Message(sender: String, recipient: String, body: String) | ||
val message4 = Message("[email protected]", "[email protected]", "Me zo o komz gant ma amezeg") | ||
|
@@ -53,6 +81,10 @@ message5.sender // [email protected] | |
message5.recipient // [email protected] | ||
message5.body // "Me zo o komz gant ma amezeg" | ||
``` | ||
{% endtab %} | ||
|
||
{% endtabs %} | ||
|
||
The recipient of `message4` is used as the sender of `message5` but the `body` of `message4` was copied directly. | ||
|
||
## More resources | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.