From 17483d40b644dae9b2b8019e5c03dbb369b576f7 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Thu, 6 Oct 2022 12:39:57 +0800 Subject: [PATCH 1/5] add code tabs in num12. --- _overviews/scala3-book/taste-objects.md | 72 ++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/_overviews/scala3-book/taste-objects.md b/_overviews/scala3-book/taste-objects.md index 6d19b9d2d5..b6d16c2c1a 100644 --- a/_overviews/scala3-book/taste-objects.md +++ b/_overviews/scala3-book/taste-objects.md @@ -19,27 +19,43 @@ Objects have several uses: In this situation, that class is also called a _companion class_. - They’re used to implement traits to create _modules_. - - ## “Utility” methods Because an `object` is a Singleton, its methods can be accessed like `static` methods in a Java class. For example, this `StringUtils` object contains a small collection of string-related methods: + +{% tabs object_1 class=tabs-scala-version %} +{% tab 'Scala 2' for=object_1 %} +```scala +object StringUtils { + def isNullOrEmpty(s: String): Boolean = s == null || s.trim.isEmpty + def leftTrim(s: String): String = s.replaceAll("^\\s+", "") + def rightTrim(s: String): String = s.replaceAll("\\s+$", "") +} +``` +{% endtab %} + +{% tab 'Scala 3' for=object_1 %} ```scala object StringUtils: def isNullOrEmpty(s: String): Boolean = s == null || s.trim.isEmpty def leftTrim(s: String): String = s.replaceAll("^\\s+", "") def rightTrim(s: String): String = s.replaceAll("\\s+$", "") ``` +{% endtab %} +{% endtabs %} Because `StringUtils` is a singleton, its methods can be called directly on the object: +{% tabs object_2 class=tabs-scala-version %} +{% tab 'Scala 2 and 3' for=object_2 %} ```scala val x = StringUtils.isNullOrEmpty("") // true val x = StringUtils.isNullOrEmpty("a") // false ``` - +{% endtab %} +{% endtabs %} ## Companion objects @@ -48,6 +64,27 @@ Use a companion object for methods and values which aren’t specific to instanc This example demonstrates how the `area` method in the companion class can access the private `calculateArea` method in its companion object: +{% tabs object_3 class=tabs-scala-version %} +{% tab 'Scala 2' for=object_3 %} +```scala +import scala.math._ + +class Circle(radius: Double) { + import Circle.__ + def area: Double = calculateArea(radius) +} + +object Circle { + private def calculateArea(radius: Double): Double = + Pi * pow(radius, 2.0) +} + +val circle1 = Circle(5.0) +circle1.area // Double = 78.53981633974483 +``` +{% endtab %} + +{% tab 'Scala 3' for=object_3 %} ```scala import scala.math.* @@ -62,13 +99,36 @@ object Circle: val circle1 = Circle(5.0) circle1.area // Double = 78.53981633974483 ``` - +{% endtab %} +{% endtabs %} ## Creating modules from traits Objects can also be used to implement traits to create modules. This technique takes two traits and combines them to create a concrete `object`: +{% tabs object_4 class=tabs-scala-version %} +{% tab 'Scala 2' for=object_4 %} +```scala +trait AddService { + def add(a: Int, b: Int) = a + b +} + +trait MultiplyService { + def multiply(a: Int, b: Int) = a * b +} + +// implement those traits as a concrete object +object MathService extends AddService, MultiplyService + +// use the object +import MathService._ +println(add(1,1)) // 2 +println(multiply(2,2)) // 4 +``` +{% endtab %} + +{% tab 'Scala 3' for=object_4 %} ```scala trait AddService: def add(a: Int, b: Int) = a + b @@ -84,6 +144,8 @@ import MathService.* println(add(1,1)) // 2 println(multiply(2,2)) // 4 ``` +{% endtab %} +{% endtabs %} {% comment %} NOTE: I don’t know if this is worth keeping, but I’m leaving it here as a comment for now. @@ -91,5 +153,3 @@ NOTE: I don’t know if this is worth keeping, but I’m leaving it here as a co > You may read that objects are used to _reify_ traits into modules. > _Reify_ means, “to take an abstract concept and turn it into something concrete.” This is what happens in these examples, but “implement” is a more familiar word for most people than “reify.” {% endcomment %} - - From 550bee5bfa1fbd648ceabbee2c8876f57f109777 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:39:12 +0800 Subject: [PATCH 2/5] Update _overviews/scala3-book/taste-objects.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-objects.md b/_overviews/scala3-book/taste-objects.md index b6d16c2c1a..6038a80112 100644 --- a/_overviews/scala3-book/taste-objects.md +++ b/_overviews/scala3-book/taste-objects.md @@ -48,7 +48,7 @@ object StringUtils: Because `StringUtils` is a singleton, its methods can be called directly on the object: -{% tabs object_2 class=tabs-scala-version %} +{% tabs object_2 %} {% tab 'Scala 2 and 3' for=object_2 %} ```scala val x = StringUtils.isNullOrEmpty("") // true From f365560c3c8819fa7901de333ed0d63b965553a2 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sat, 15 Oct 2022 02:02:27 +0800 Subject: [PATCH 3/5] Update _overviews/scala3-book/taste-objects.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-objects.md b/_overviews/scala3-book/taste-objects.md index 6038a80112..683f32a1c2 100644 --- a/_overviews/scala3-book/taste-objects.md +++ b/_overviews/scala3-book/taste-objects.md @@ -79,7 +79,7 @@ object Circle { Pi * pow(radius, 2.0) } -val circle1 = Circle(5.0) +val circle1 = new Circle(5.0) circle1.area // Double = 78.53981633974483 ``` {% endtab %} From 724e0bb306bc4bd055bfcf5c47e9d7db244b178e Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sat, 15 Oct 2022 02:02:36 +0800 Subject: [PATCH 4/5] Update _overviews/scala3-book/taste-objects.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-objects.md b/_overviews/scala3-book/taste-objects.md index 683f32a1c2..cef1ae2027 100644 --- a/_overviews/scala3-book/taste-objects.md +++ b/_overviews/scala3-book/taste-objects.md @@ -70,7 +70,7 @@ This example demonstrates how the `area` method in the companion class can acces import scala.math._ class Circle(radius: Double) { - import Circle.__ + import Circle._ def area: Double = calculateArea(radius) } From 7404d59ad8520ebc8c288821c9e7a7dc10033a1a Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sat, 15 Oct 2022 02:02:53 +0800 Subject: [PATCH 5/5] Update _overviews/scala3-book/taste-objects.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-objects.md b/_overviews/scala3-book/taste-objects.md index cef1ae2027..0de96664ea 100644 --- a/_overviews/scala3-book/taste-objects.md +++ b/_overviews/scala3-book/taste-objects.md @@ -119,7 +119,7 @@ trait MultiplyService { } // implement those traits as a concrete object -object MathService extends AddService, MultiplyService +object MathService extends AddService with MultiplyService // use the object import MathService._