From d29b89aecf502f5e5b147d5528c184b7d4b1b4d4 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Fri, 16 Sep 2022 21:51:26 +0200 Subject: [PATCH 1/4] Add code tabs for _tour/case-classes --- _tour/case-classes.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/_tour/case-classes.md b/_tour/case-classes.md index b05756f81b..a7890076f0 100644 --- a/_tour/case-classes.md +++ b/_tour/case-classes.md @@ -15,14 +15,26 @@ 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("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") @@ -30,10 +42,18 @@ val message1 = Message("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") println(message1.sender) // prints guillaume@quebec.ca message1.sender = "travis@washington.us" // 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 %} ```scala mdoc case class Message(sender: String, recipient: String, body: String) @@ -41,10 +61,18 @@ val message2 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?") val message3 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "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 %} ```scala mdoc:nest case class Message(sender: String, recipient: String, body: String) val message4 = Message("julien@bretagne.fr", "travis@washington.us", "Me zo o komz gant ma amezeg") @@ -53,6 +81,10 @@ message5.sender // travis@washington.us message5.recipient // claire@bourgogne.fr 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 From ce3fb7eefd705eead670747fdb5940a8ebd0c885 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Tue, 20 Sep 2022 18:07:48 +0200 Subject: [PATCH 2/4] Update _tour/case-classes.md Co-authored-by: Jamie Thompson --- _tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/case-classes.md b/_tour/case-classes.md index a7890076f0..e9319af030 100644 --- a/_tour/case-classes.md +++ b/_tour/case-classes.md @@ -34,7 +34,7 @@ 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 %} +{% tab 'Scala 2 and 3' for=case-classe_Message_define %} ``` case class Message(sender: String, recipient: String, body: String) val message1 = Message("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") From ca52f7abf4f06c663651aea3be6d37b10a695650 Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Tue, 20 Sep 2022 18:08:54 +0200 Subject: [PATCH 3/4] Update _tour/case-classes.md Co-authored-by: Jamie Thompson --- _tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/case-classes.md b/_tour/case-classes.md index e9319af030..d1aef05c5f 100644 --- a/_tour/case-classes.md +++ b/_tour/case-classes.md @@ -72,7 +72,7 @@ You can create a (shallow) copy of an instance of a case class simply by using t {% tabs case-classe_Message_copy %} -{% tab 'Scala 2 and 3' for=case-Message_copy %} +{% tab 'Scala 2 and 3' for=case-classe_Message_copy %} ```scala mdoc:nest case class Message(sender: String, recipient: String, body: String) val message4 = Message("julien@bretagne.fr", "travis@washington.us", "Me zo o komz gant ma amezeg") From f3512f69dba749274aa734005964c96c5238860d Mon Sep 17 00:00:00 2001 From: Luc Henninger Date: Tue, 20 Sep 2022 18:10:20 +0200 Subject: [PATCH 4/4] Update case-classes.md --- _tour/case-classes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/case-classes.md b/_tour/case-classes.md index d1aef05c5f..2386fb0ead 100644 --- a/_tour/case-classes.md +++ b/_tour/case-classes.md @@ -53,7 +53,7 @@ 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 %} +{% tab 'Scala 2 and 3' for=case-classe_Message_compare %} ```scala mdoc case class Message(sender: String, recipient: String, body: String)