@@ -19,27 +19,43 @@ Objects have several uses:
19
19
In this situation, that class is also called a _ companion class_ .
20
20
- They’re used to implement traits to create _ modules_ .
21
21
22
-
23
-
24
22
## “Utility” methods
25
23
26
24
Because an ` object ` is a Singleton, its methods can be accessed like ` static ` methods in a Java class.
27
25
For example, this ` StringUtils ` object contains a small collection of string-related methods:
28
26
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 %}
29
40
``` scala
30
41
object StringUtils :
31
42
def isNullOrEmpty (s : String ): Boolean = s == null || s.trim.isEmpty
32
43
def leftTrim (s : String ): String = s.replaceAll(" ^\\ s+" , " " )
33
44
def rightTrim (s : String ): String = s.replaceAll(" \\ s+$" , " " )
34
45
```
46
+ {% endtab %}
47
+ {% endtabs %}
35
48
36
49
Because ` StringUtils ` is a singleton, its methods can be called directly on the object:
37
50
51
+ {% tabs object_2 %}
52
+ {% tab 'Scala 2 and 3' for=object_2 %}
38
53
``` scala
39
54
val x = StringUtils .isNullOrEmpty(" " ) // true
40
55
val x = StringUtils .isNullOrEmpty(" a" ) // false
41
56
```
42
-
57
+ {% endtab %}
58
+ {% endtabs %}
43
59
44
60
## Companion objects
45
61
@@ -48,6 +64,27 @@ Use a companion object for methods and values which aren’t specific to instanc
48
64
49
65
This example demonstrates how the ` area ` method in the companion class can access the private ` calculateArea ` method in its companion object:
50
66
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 %}
51
88
``` scala
52
89
import scala .math .*
53
90
@@ -62,13 +99,36 @@ object Circle:
62
99
val circle1 = Circle (5.0 )
63
100
circle1.area // Double = 78.53981633974483
64
101
```
65
-
102
+ {% endtab %}
103
+ {% endtabs %}
66
104
67
105
## Creating modules from traits
68
106
69
107
Objects can also be used to implement traits to create modules.
70
108
This technique takes two traits and combines them to create a concrete ` object ` :
71
109
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 %}
72
132
``` scala
73
133
trait AddService :
74
134
def add (a : Int , b : Int ) = a + b
@@ -84,12 +144,12 @@ import MathService.*
84
144
println(add(1 ,1 )) // 2
85
145
println(multiply(2 ,2 )) // 4
86
146
```
147
+ {% endtab %}
148
+ {% endtabs %}
87
149
88
150
{% comment %}
89
151
NOTE: I don’t know if this is worth keeping, but I’m leaving it here as a comment for now.
90
152
91
153
> You may read that objects are used to _ reify_ traits into modules.
92
154
> _ 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.”
93
155
{% endcomment %}
94
-
95
-
0 commit comments