@@ -14,99 +14,120 @@ next-page: taste-functions
14
14
Scala classes, case classes, traits, enums, and objects can all contain methods.
15
15
The syntax of a simple method looks like this:
16
16
17
+ {% tabs method_1 %}
18
+ {% tab 'Scala 2 and 3' for=method_1 %}
17
19
``` scala
18
20
def methodName (param1 : Type1 , param2 : Type2 ): ReturnType =
19
21
// the method body
20
22
// goes here
21
23
```
24
+ {% endtab %}
25
+ {% endtabs %}
22
26
23
27
Here are a few examples:
24
28
29
+ {% tabs method_2 %}
30
+ {% tab 'Scala 2 and 3' for=method_2 %}
25
31
``` scala
26
32
def sum (a : Int , b : Int ): Int = a + b
27
33
def concatenate (s1 : String , s2 : String ): String = s1 + s2
28
34
```
35
+ {% endtab %}
36
+ {% endtabs %}
29
37
30
38
You don’t have to declare a method’s return type, so you can write those methods like this, if you prefer:
31
39
40
+ {% tabs method_3 %}
41
+ {% tab 'Scala 2 and 3' for=method_3 %}
32
42
``` scala
33
43
def sum (a : Int , b : Int ) = a + b
34
44
def concatenate (s1 : String , s2 : String ) = s1 + s2
35
45
```
46
+ {% endtab %}
47
+ {% endtabs %}
36
48
37
49
This is how you call those methods:
38
50
51
+ {% tabs method_4 %}
52
+ {% tab 'Scala 2 and 3' for=method_4 %}
39
53
``` scala
40
54
val x = sum(1 , 2 )
41
55
val y = concatenate(" foo" , " bar" )
42
56
```
57
+ {% endtab %}
58
+ {% endtabs %}
43
59
44
60
Here’s an example of a multiline method:
45
61
62
+ {% tabs method_5 class=tabs-scala-version %}
63
+ {% tab 'Scala 2' for=method_5 %}
64
+ ``` scala
65
+ def getStackTraceAsString (t : Throwable ): String = {
66
+ val sw = new StringWriter
67
+ t.printStackTrace(new PrintWriter (sw))
68
+ sw.toString
69
+ }
70
+ ```
71
+ {% endtab %}
72
+
73
+ {% tab 'Scala 3' for=method_5 %}
46
74
``` scala
47
75
def getStackTraceAsString (t : Throwable ): String =
48
76
val sw = new StringWriter
49
77
t.printStackTrace(new PrintWriter (sw))
50
78
sw.toString
51
79
```
80
+ {% endtab %}
81
+ {% endtabs %}
52
82
53
83
Method parameters can also have default values.
54
84
In this example, the ` timeout ` parameter has a default value of ` 5000 ` :
55
85
86
+ {% tabs method_6 %}
87
+ {% tab 'Scala 2 and 3' for=method_6 %}
56
88
``` scala
57
89
def makeConnection (url : String , timeout : Int = 5000 ): Unit =
58
90
println(s " url= $url, timeout= $timeout" )
59
91
```
92
+ {% endtab %}
93
+ {% endtabs %}
60
94
61
95
Because a default ` timeout ` value is supplied in the method declaration, the method can be called in these two ways:
62
96
97
+ {% tabs method_7 %}
98
+ {% tab 'Scala 2 and 3' for=method_7 %}
63
99
``` scala
64
100
makeConnection(" https://localhost" ) // url=http://localhost, timeout=5000
65
101
makeConnection(" https://localhost" , 2500 ) // url=http://localhost, timeout=2500
66
102
```
103
+ {% endtab %}
104
+ {% endtabs %}
67
105
68
106
Scala also supports the use of _ named parameters_ when calling a method, so you can also call that method like this, if you prefer:
69
107
108
+ {% tabs method_8 %}
109
+ {% tab 'Scala 2 and 3' for=method_8 %}
70
110
``` scala
71
111
makeConnection(
72
112
url = " https://localhost" ,
73
113
timeout = 2500
74
114
)
75
115
```
116
+ {% endtab %}
117
+ {% endtabs %}
76
118
77
119
Named parameters are particularly useful when multiple method parameters have the same type.
78
120
At a glance, with this method you may wonder which parameters are set to ` true ` or ` false ` :
79
121
80
- ``` scala
81
- engage(true , true , true , false )
82
- ```
83
-
84
- Without help from an IDE that code can be hard to read, but this code is much more obvious:
122
+ {% tabs method_9 %}
123
+ {% tab 'Scala 2 and 3' for=method_9 %}
85
124
86
125
``` scala
87
- engage(
88
- speedIsSet = true ,
89
- directionIsSet = true ,
90
- picardSaidMakeItSo = true ,
91
- turnedOffParkingBrake = false
92
- )
126
+ engage(true , true , true , false )
93
127
```
94
128
95
-
96
-
97
- ## Extension methods
98
-
99
- _ Extension methods_ let you add new methods to closed classes.
100
- For instance, if you want to add two methods named ` hello ` and ` aloha ` to the ` String ` class, declare them as extension methods:
101
-
102
- ``` scala
103
- extension (s : String )
104
- def hello : String = s " Hello, ${s.capitalize}! "
105
- def aloha : String = s " Aloha, ${s.capitalize}! "
106
-
107
- " world" .hello // "Hello, World!"
108
- " friend" .aloha // "Aloha, Friend!"
109
- ```
129
+ {% endtab %}
130
+ {% endtabs %}
110
131
111
132
The ` extension ` keyword declares that you’re about to define one or more extension methods on the parameter that’s put in parentheses.
112
133
As shown with this example, the parameter ` s ` of type ` String ` can then be used in the body of your extension methods.
@@ -115,6 +136,9 @@ This next example shows how to add a `makeInt` method to the `String` class.
115
136
Here, ` makeInt ` takes a parameter named ` radix ` .
116
137
The code doesn’t account for possible string-to-integer conversion errors, but skipping that detail, the examples show how it works:
117
138
139
+ {% tabs extension_2 class=tabs-scala-version %}
140
+ {% tab 'Scala 3 Only' for=extension_2 %}
141
+
118
142
``` scala
119
143
extension (s : String )
120
144
def makeInt (radix : Int ): Int = Integer .parseInt(s, radix)
@@ -124,13 +148,12 @@ extension (s: String)
124
148
" 100" .makeInt(2 ) // Int = 4
125
149
```
126
150
127
-
151
+ {% endtab %}
152
+ {% endtabs %}
128
153
129
154
## See also
130
155
131
156
Scala Methods can be much more powerful: they can take type parameters and context parameters.
132
157
They are covered in detail in the [ Domain Modeling] [ data-1 ] section.
133
158
134
-
135
-
136
159
[ data-1] : {% link _ overviews/scala3-book/domain-modeling-tools.md %}
0 commit comments