Skip to content

Commit 9d16ae0

Browse files
authored
Merge pull request #2582 from benluo/_overview/scala3-book/num10-code-tabs
add code tabs in num10.
2 parents ee62e90 + 80d9f7c commit 9d16ae0

File tree

1 file changed

+52
-29
lines changed

1 file changed

+52
-29
lines changed

_overviews/scala3-book/taste-methods.md

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,99 +14,120 @@ 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 %}
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 %}
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 %}
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 %}
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 %}
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 %}
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 %}
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

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 %}
85124

86125
```scala
87-
engage(
88-
speedIsSet = true,
89-
directionIsSet = true,
90-
picardSaidMakeItSo = true,
91-
turnedOffParkingBrake = false
92-
)
126+
engage(true, true, true, false)
93127
```
94128

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 %}
110131

111132
The `extension` keyword declares that you’re about to define one or more extension methods on the parameter that’s put in parentheses.
112133
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.
115136
Here, `makeInt` takes a parameter named `radix`.
116137
The code doesn’t account for possible string-to-integer conversion errors, but skipping that detail, the examples show how it works:
117138

139+
{% tabs extension_2 class=tabs-scala-version %}
140+
{% tab 'Scala 3 Only' for=extension_2 %}
141+
118142
```scala
119143
extension (s: String)
120144
def makeInt(radix: Int): Int = Integer.parseInt(s, radix)
@@ -124,13 +148,12 @@ extension (s: String)
124148
"100".makeInt(2) // Int = 4
125149
```
126150

127-
151+
{% endtab %}
152+
{% endtabs %}
128153

129154
## See also
130155

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

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

0 commit comments

Comments
 (0)