Skip to content

Commit b202896

Browse files
committed
correct 2&3 and 3 Only.
1 parent 5a5021c commit b202896

File tree

1 file changed

+7
-37
lines changed

1 file changed

+7
-37
lines changed

_overviews/scala3-book/taste-methods.md

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ 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 %}
17+
{% tabs method_1 %}
1818
{% tab 'Scala 2 and 3' for=method_1 %}
1919
```scala
2020
def methodName(param1: Type1, param2: Type2): ReturnType =
@@ -26,7 +26,7 @@ def methodName(param1: Type1, param2: Type2): ReturnType =
2626

2727
Here are a few examples:
2828

29-
{% tabs method_2 class=tabs-scala-version %}
29+
{% tabs method_2 %}
3030
{% tab 'Scala 2 and 3' for=method_2 %}
3131
```scala
3232
def sum(a: Int, b: Int): Int = a + b
@@ -37,7 +37,7 @@ def concatenate(s1: String, s2: String): String = s1 + s2
3737

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

40-
{% tabs method_3 class=tabs-scala-version %}
40+
{% tabs method_3 %}
4141
{% tab 'Scala 2 and 3' for=method_3 %}
4242
```scala
4343
def sum(a: Int, b: Int) = a + b
@@ -48,7 +48,7 @@ def concatenate(s1: String, s2: String) = s1 + s2
4848

4949
This is how you call those methods:
5050

51-
{% tabs method_4 class=tabs-scala-version %}
51+
{% tabs method_4 %}
5252
{% tab 'Scala 2 and 3' for=method_4 %}
5353
```scala
5454
val x = sum(1, 2)
@@ -83,7 +83,7 @@ def getStackTraceAsString(t: Throwable): String =
8383
Method parameters can also have default values.
8484
In this example, the `timeout` parameter has a default value of `5000`:
8585

86-
{% tabs method_6 class=tabs-scala-version %}
86+
{% tabs method_6 %}
8787
{% tab 'Scala 2 and 3' for=method_6 %}
8888
```scala
8989
def makeConnection(url: String, timeout: Int = 5000): Unit =
@@ -94,7 +94,7 @@ def makeConnection(url: String, timeout: Int = 5000): Unit =
9494

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

97-
{% tabs method_7 class=tabs-scala-version %}
97+
{% tabs method_7 %}
9898
{% tab 'Scala 2 and 3' for=method_7 %}
9999
```scala
100100
makeConnection("https://localhost") // url=http://localhost, timeout=5000
@@ -105,7 +105,7 @@ makeConnection("https://localhost", 2500) // url=http://localhost, timeout=250
105105

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

108-
{% tabs method_8 class=tabs-scala-version %}
108+
{% tabs method_8 %}
109109
{% tab 'Scala 2 and 3' for=method_8 %}
110110
```scala
111111
makeConnection(
@@ -126,36 +126,6 @@ engage(true, true, true, false)
126126
```
127127
{% endtab %}
128128
{% 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!"
159129
```
160130
{% endtab %}
161131
{% endtabs %}

0 commit comments

Comments
 (0)