Skip to content

Commit 30da297

Browse files
committed
add code tabs in num10.
1 parent 69a362b commit 30da297

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

_overviews/scala3-book/taste-methods.md

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,75 +14,123 @@ next-page: taste-functions
1414
Scala classes, case classes, traits, enums, and objects can all contain methods.
1515
The syntax of a simple method looks like this:
1616

17+
{% tabs method_1 class=tabs-scala-version %}
18+
{% tab 'Scala 2 and 3' for=method_1 %}
1719
```scala
1820
def methodName(param1: Type1, param2: Type2): ReturnType =
1921
// the method body
2022
// goes here
2123
```
24+
{% endtab %}
25+
{% endtabs %}
2226

2327
Here are a few examples:
2428

29+
{% tabs method_2 class=tabs-scala-version %}
30+
{% tab 'Scala 2 and 3' for=method_2 %}
2531
```scala
2632
def sum(a: Int, b: Int): Int = a + b
2733
def concatenate(s1: String, s2: String): String = s1 + s2
2834
```
35+
{% endtab %}
36+
{% endtabs %}
2937

3038
You don’t have to declare a method’s return type, so you can write those methods like this, if you prefer:
3139

40+
{% tabs method_3 class=tabs-scala-version %}
41+
{% tab 'Scala 2 and 3' for=method_3 %}
3242
```scala
3343
def sum(a: Int, b: Int) = a + b
3444
def concatenate(s1: String, s2: String) = s1 + s2
3545
```
46+
{% endtab %}
47+
{% endtabs %}
3648

3749
This is how you call those methods:
3850

51+
{% tabs method_4 class=tabs-scala-version %}
52+
{% tab 'Scala 2 and 3' for=method_4 %}
3953
```scala
4054
val x = sum(1, 2)
4155
val y = concatenate("foo", "bar")
4256
```
57+
{% endtab %}
58+
{% endtabs %}
4359

4460
Here’s an example of a multiline method:
4561

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 %}
4674
```scala
4775
def getStackTraceAsString(t: Throwable): String =
4876
val sw = new StringWriter
4977
t.printStackTrace(new PrintWriter(sw))
5078
sw.toString
5179
```
80+
{% endtab %}
81+
{% endtabs %}
5282

5383
Method parameters can also have default values.
5484
In this example, the `timeout` parameter has a default value of `5000`:
5585

86+
{% tabs method_6 class=tabs-scala-version %}
87+
{% tab 'Scala 2 and 3' for=method_6 %}
5688
```scala
5789
def makeConnection(url: String, timeout: Int = 5000): Unit =
5890
println(s"url=$url, timeout=$timeout")
5991
```
92+
{% endtab %}
93+
{% endtabs %}
6094

6195
Because a default `timeout` value is supplied in the method declaration, the method can be called in these two ways:
6296

97+
{% tabs method_7 class=tabs-scala-version %}
98+
{% tab 'Scala 2 and 3' for=method_7 %}
6399
```scala
64100
makeConnection("https://localhost") // url=http://localhost, timeout=5000
65101
makeConnection("https://localhost", 2500) // url=http://localhost, timeout=2500
66102
```
103+
{% endtab %}
104+
{% endtabs %}
67105

68106
Scala also supports the use of _named parameters_ when calling a method, so you can also call that method like this, if you prefer:
69107

108+
{% tabs method_8 class=tabs-scala-version %}
109+
{% tab 'Scala 2 and 3' for=method_8 %}
70110
```scala
71111
makeConnection(
72112
url = "https://localhost",
73113
timeout = 2500
74114
)
75115
```
116+
{% endtab %}
117+
{% endtabs %}
76118

77119
Named parameters are particularly useful when multiple method parameters have the same type.
78120
At a glance, with this method you may wonder which parameters are set to `true` or `false`:
79121

122+
{% tabs method_9 class=tabs-scala-version %}
123+
{% tab 'Scala 2 and 3' for=method_9 %}
80124
```scala
81125
engage(true, true, true, false)
82126
```
127+
{% endtab %}
128+
{% endtabs %}
83129

84130
Without help from an IDE that code can be hard to read, but this code is much more obvious:
85131

132+
{% tabs method_10 class=tabs-scala-version %}
133+
{% tab 'Scala 2 and 3' for=method_10 %}
86134
```scala
87135
engage(
88136
speedIsSet = true,
@@ -91,14 +139,16 @@ engage(
91139
turnedOffParkingBrake = false
92140
)
93141
```
94-
95-
142+
{% endtab %}
143+
{% endtabs %}
96144

97145
## Extension methods
98146

99147
_Extension methods_ let you add new methods to closed classes.
100148
For instance, if you want to add two methods named `hello` and `aloha` to the `String` class, declare them as extension methods:
101149

150+
{% tabs extension_1 class=tabs-scala-version %}
151+
{% tab 'Scala 3 only' for=extension_1 %}
102152
```scala
103153
extension (s: String)
104154
def hello: String = s"Hello, ${s.capitalize}!"
@@ -107,6 +157,8 @@ extension (s: String)
107157
"world".hello // "Hello, World!"
108158
"friend".aloha // "Aloha, Friend!"
109159
```
160+
{% endtab %}
161+
{% endtabs %}
110162

111163
The `extension` keyword declares that you’re about to define one or more extension methods on the parameter that’s put in parentheses.
112164
As shown with this example, the parameter `s` of type `String` can then be used in the body of your extension methods.
@@ -115,6 +167,8 @@ This next example shows how to add a `makeInt` method to the `String` class.
115167
Here, `makeInt` takes a parameter named `radix`.
116168
The code doesn’t account for possible string-to-integer conversion errors, but skipping that detail, the examples show how it works:
117169

170+
{% tabs extension_2 class=tabs-scala-version %}
171+
{% tab 'Scala 3 only' for=extension_2 %}
118172
```scala
119173
extension (s: String)
120174
def makeInt(radix: Int): Int = Integer.parseInt(s, radix)
@@ -123,14 +177,12 @@ extension (s: String)
123177
"10".makeInt(2) // Int = 2
124178
"100".makeInt(2) // Int = 4
125179
```
126-
127-
180+
{% endtab %}
181+
{% endtabs %}
128182

129183
## See also
130184

131185
Scala Methods can be much more powerful: they can take type parameters and context parameters.
132186
They are covered in detail in the [Domain Modeling][data-1] section.
133187

134-
135-
136188
[data-1]: {% link _overviews/scala3-book/domain-modeling-tools.md %}

0 commit comments

Comments
 (0)