Skip to content

Commit a5374ef

Browse files
authored
Merge pull request #2584 from benluo/_overview/scala3-book/num12-code-tabs
add code tabs in num12.
2 parents 55f9e70 + ab47515 commit a5374ef

File tree

1 file changed

+66
-6
lines changed

1 file changed

+66
-6
lines changed

_overviews/scala3-book/taste-objects.md

+66-6
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,43 @@ Objects have several uses:
1919
In this situation, that class is also called a _companion class_.
2020
- They’re used to implement traits to create _modules_.
2121

22-
23-
2422
## “Utility” methods
2523

2624
Because an `object` is a Singleton, its methods can be accessed like `static` methods in a Java class.
2725
For example, this `StringUtils` object contains a small collection of string-related methods:
2826

27+
28+
{% tabs object_1 class=tabs-scala-version %}
29+
{% tab 'Scala 2' for=object_1 %}
30+
```scala
31+
object StringUtils {
32+
def isNullOrEmpty(s: String): Boolean = s == null || s.trim.isEmpty
33+
def leftTrim(s: String): String = s.replaceAll("^\\s+", "")
34+
def rightTrim(s: String): String = s.replaceAll("\\s+$", "")
35+
}
36+
```
37+
{% endtab %}
38+
39+
{% tab 'Scala 3' for=object_1 %}
2940
```scala
3041
object StringUtils:
3142
def isNullOrEmpty(s: String): Boolean = s == null || s.trim.isEmpty
3243
def leftTrim(s: String): String = s.replaceAll("^\\s+", "")
3344
def rightTrim(s: String): String = s.replaceAll("\\s+$", "")
3445
```
46+
{% endtab %}
47+
{% endtabs %}
3548

3649
Because `StringUtils` is a singleton, its methods can be called directly on the object:
3750

51+
{% tabs object_2 %}
52+
{% tab 'Scala 2 and 3' for=object_2 %}
3853
```scala
3954
val x = StringUtils.isNullOrEmpty("") // true
4055
val x = StringUtils.isNullOrEmpty("a") // false
4156
```
42-
57+
{% endtab %}
58+
{% endtabs %}
4359

4460
## Companion objects
4561

@@ -48,6 +64,27 @@ Use a companion object for methods and values which aren’t specific to instanc
4864

4965
This example demonstrates how the `area` method in the companion class can access the private `calculateArea` method in its companion object:
5066

67+
{% tabs object_3 class=tabs-scala-version %}
68+
{% tab 'Scala 2' for=object_3 %}
69+
```scala
70+
import scala.math._
71+
72+
class Circle(radius: Double) {
73+
import Circle._
74+
def area: Double = calculateArea(radius)
75+
}
76+
77+
object Circle {
78+
private def calculateArea(radius: Double): Double =
79+
Pi * pow(radius, 2.0)
80+
}
81+
82+
val circle1 = new Circle(5.0)
83+
circle1.area // Double = 78.53981633974483
84+
```
85+
{% endtab %}
86+
87+
{% tab 'Scala 3' for=object_3 %}
5188
```scala
5289
import scala.math.*
5390

@@ -62,13 +99,36 @@ object Circle:
6299
val circle1 = Circle(5.0)
63100
circle1.area // Double = 78.53981633974483
64101
```
65-
102+
{% endtab %}
103+
{% endtabs %}
66104

67105
## Creating modules from traits
68106

69107
Objects can also be used to implement traits to create modules.
70108
This technique takes two traits and combines them to create a concrete `object`:
71109

110+
{% tabs object_4 class=tabs-scala-version %}
111+
{% tab 'Scala 2' for=object_4 %}
112+
```scala
113+
trait AddService {
114+
def add(a: Int, b: Int) = a + b
115+
}
116+
117+
trait MultiplyService {
118+
def multiply(a: Int, b: Int) = a * b
119+
}
120+
121+
// implement those traits as a concrete object
122+
object MathService extends AddService with MultiplyService
123+
124+
// use the object
125+
import MathService._
126+
println(add(1,1)) // 2
127+
println(multiply(2,2)) // 4
128+
```
129+
{% endtab %}
130+
131+
{% tab 'Scala 3' for=object_4 %}
72132
```scala
73133
trait AddService:
74134
def add(a: Int, b: Int) = a + b
@@ -84,12 +144,12 @@ import MathService.*
84144
println(add(1,1)) // 2
85145
println(multiply(2,2)) // 4
86146
```
147+
{% endtab %}
148+
{% endtabs %}
87149

88150
{% comment %}
89151
NOTE: I don’t know if this is worth keeping, but I’m leaving it here as a comment for now.
90152

91153
> You may read that objects are used to _reify_ traits into modules.
92154
> _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.”
93155
{% endcomment %}
94-
95-

0 commit comments

Comments
 (0)