@@ -14,7 +14,7 @@ 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 class=tabs-scala-version %}
17
+ {% tabs method_1 %}
18
18
{% tab 'Scala 2 and 3' for=method_1 %}
19
19
``` scala
20
20
def methodName (param1 : Type1 , param2 : Type2 ): ReturnType =
@@ -26,7 +26,7 @@ def methodName(param1: Type1, param2: Type2): ReturnType =
26
26
27
27
Here are a few examples:
28
28
29
- {% tabs method_2 class=tabs-scala-version %}
29
+ {% tabs method_2 %}
30
30
{% tab 'Scala 2 and 3' for=method_2 %}
31
31
``` scala
32
32
def sum (a : Int , b : Int ): Int = a + b
@@ -37,7 +37,7 @@ def concatenate(s1: String, s2: String): String = s1 + s2
37
37
38
38
You don’t have to declare a method’s return type, so you can write those methods like this, if you prefer:
39
39
40
- {% tabs method_3 class=tabs-scala-version %}
40
+ {% tabs method_3 %}
41
41
{% tab 'Scala 2 and 3' for=method_3 %}
42
42
``` scala
43
43
def sum (a : Int , b : Int ) = a + b
@@ -48,7 +48,7 @@ def concatenate(s1: String, s2: String) = s1 + s2
48
48
49
49
This is how you call those methods:
50
50
51
- {% tabs method_4 class=tabs-scala-version %}
51
+ {% tabs method_4 %}
52
52
{% tab 'Scala 2 and 3' for=method_4 %}
53
53
``` scala
54
54
val x = sum(1 , 2 )
@@ -83,7 +83,7 @@ def getStackTraceAsString(t: Throwable): String =
83
83
Method parameters can also have default values.
84
84
In this example, the ` timeout ` parameter has a default value of ` 5000 ` :
85
85
86
- {% tabs method_6 class=tabs-scala-version %}
86
+ {% tabs method_6 %}
87
87
{% tab 'Scala 2 and 3' for=method_6 %}
88
88
``` scala
89
89
def makeConnection (url : String , timeout : Int = 5000 ): Unit =
@@ -94,7 +94,7 @@ def makeConnection(url: String, timeout: Int = 5000): Unit =
94
94
95
95
Because a default ` timeout ` value is supplied in the method declaration, the method can be called in these two ways:
96
96
97
- {% tabs method_7 class=tabs-scala-version %}
97
+ {% tabs method_7 %}
98
98
{% tab 'Scala 2 and 3' for=method_7 %}
99
99
``` scala
100
100
makeConnection(" https://localhost" ) // url=http://localhost, timeout=5000
@@ -105,7 +105,7 @@ makeConnection("https://localhost", 2500) // url=http://localhost, timeout=250
105
105
106
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:
107
107
108
- {% tabs method_8 class=tabs-scala-version %}
108
+ {% tabs method_8 %}
109
109
{% tab 'Scala 2 and 3' for=method_8 %}
110
110
``` scala
111
111
makeConnection(
@@ -126,36 +126,6 @@ engage(true, true, true, false)
126
126
```
127
127
{% endtab %}
128
128
{% endtabs %}
129
-
130
- Without help from an IDE that code can be hard to read, but this code is much more obvious:
131
-
132
- {% tabs method_10 class=tabs-scala-version %}
133
- {% tab 'Scala 2 and 3' for=method_10 %}
134
- ``` scala
135
- engage(
136
- speedIsSet = true ,
137
- directionIsSet = true ,
138
- picardSaidMakeItSo = true ,
139
- turnedOffParkingBrake = false
140
- )
141
- ```
142
- {% endtab %}
143
- {% endtabs %}
144
-
145
- ## Extension methods
146
-
147
- _ Extension methods_ let you add new methods to closed classes.
148
- For instance, if you want to add two methods named ` hello ` and ` aloha ` to the ` String ` class, declare them as extension methods:
149
-
150
- {% tabs extension_1 class=tabs-scala-version %}
151
- {% tab 'Scala 3 Only' for=extension_1 %}
152
- ``` scala
153
- extension (s : String )
154
- def hello : String = s " Hello, ${s.capitalize}! "
155
- def aloha : String = s " Aloha, ${s.capitalize}! "
156
-
157
- " world" .hello // "Hello, World!"
158
- " friend" .aloha // "Aloha, Friend!"
159
129
```
160
130
{% endtab %}
161
131
{% endtabs %}
0 commit comments