-
Notifications
You must be signed in to change notification settings - Fork 1k
add code tabs in num10. #2582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bishabosha
merged 6 commits into
scala:main
from
benluo:_overview/scala3-book/num10-code-tabs
Oct 7, 2022
Merged
add code tabs in num10. #2582
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
30da297
add code tabs in num10.
benluo 0153f36
Update _overviews/scala3-book/taste-methods.md
benluo d2227a9
Update _overviews/scala3-book/taste-methods.md
benluo 5a5021c
Update _overviews/scala3-book/taste-methods.md
benluo b202896
correct 2&3 and 3 Only.
benluo 80d9f7c
delete tabs
benluo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,75 +14,123 @@ next-page: taste-functions | |
Scala classes, case classes, traits, enums, and objects can all contain methods. | ||
The syntax of a simple method looks like this: | ||
|
||
{% tabs method_1 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_1 %} | ||
```scala | ||
def methodName(param1: Type1, param2: Type2): ReturnType = | ||
// the method body | ||
// goes here | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Here are a few examples: | ||
|
||
{% tabs method_2 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_2 %} | ||
```scala | ||
def sum(a: Int, b: Int): Int = a + b | ||
def concatenate(s1: String, s2: String): String = s1 + s2 | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
You don’t have to declare a method’s return type, so you can write those methods like this, if you prefer: | ||
|
||
{% tabs method_3 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_3 %} | ||
```scala | ||
def sum(a: Int, b: Int) = a + b | ||
def concatenate(s1: String, s2: String) = s1 + s2 | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
This is how you call those methods: | ||
|
||
{% tabs method_4 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_4 %} | ||
```scala | ||
val x = sum(1, 2) | ||
val y = concatenate("foo", "bar") | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Here’s an example of a multiline method: | ||
|
||
{% tabs method_5 class=tabs-scala-version %} | ||
{% tab 'Scala 2' for=method_5 %} | ||
```scala | ||
def getStackTraceAsString(t: Throwable): String = { | ||
val sw = new StringWriter | ||
t.printStackTrace(new PrintWriter(sw)) | ||
sw.toString | ||
} | ||
``` | ||
{% endtab %} | ||
|
||
{% tab 'Scala 3' for=method_5 %} | ||
```scala | ||
def getStackTraceAsString(t: Throwable): String = | ||
val sw = new StringWriter | ||
t.printStackTrace(new PrintWriter(sw)) | ||
sw.toString | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Method parameters can also have default values. | ||
In this example, the `timeout` parameter has a default value of `5000`: | ||
|
||
{% tabs method_6 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_6 %} | ||
```scala | ||
def makeConnection(url: String, timeout: Int = 5000): Unit = | ||
println(s"url=$url, timeout=$timeout") | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Because a default `timeout` value is supplied in the method declaration, the method can be called in these two ways: | ||
|
||
{% tabs method_7 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_7 %} | ||
```scala | ||
makeConnection("https://localhost") // url=http://localhost, timeout=5000 | ||
makeConnection("https://localhost", 2500) // url=http://localhost, timeout=2500 | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Scala also supports the use of _named parameters_ when calling a method, so you can also call that method like this, if you prefer: | ||
|
||
{% tabs method_8 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_8 %} | ||
```scala | ||
makeConnection( | ||
url = "https://localhost", | ||
timeout = 2500 | ||
) | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Named parameters are particularly useful when multiple method parameters have the same type. | ||
At a glance, with this method you may wonder which parameters are set to `true` or `false`: | ||
|
||
{% tabs method_9 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_9 %} | ||
```scala | ||
engage(true, true, true, false) | ||
``` | ||
{% endtab %} | ||
{% endtabs %} | ||
|
||
Without help from an IDE that code can be hard to read, but this code is much more obvious: | ||
|
||
{% tabs method_10 class=tabs-scala-version %} | ||
{% tab 'Scala 2 and 3' for=method_10 %} | ||
```scala | ||
engage( | ||
speedIsSet = true, | ||
|
@@ -91,14 +139,16 @@ engage( | |
turnedOffParkingBrake = false | ||
) | ||
``` | ||
|
||
|
||
{% endtab %} | ||
{% endtabs %} | ||
|
||
## Extension methods | ||
|
||
_Extension methods_ let you add new methods to closed classes. | ||
For instance, if you want to add two methods named `hello` and `aloha` to the `String` class, declare them as extension methods: | ||
|
||
{% tabs extension_1 class=tabs-scala-version %} | ||
{% tab 'Scala 3 only' for=extension_1 %} | ||
benluo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```scala | ||
extension (s: String) | ||
def hello: String = s"Hello, ${s.capitalize}!" | ||
|
@@ -107,6 +157,8 @@ extension (s: String) | |
"world".hello // "Hello, World!" | ||
"friend".aloha // "Aloha, Friend!" | ||
``` | ||
{% endtab %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these three lines should be removed
|
||
{% endtabs %} | ||
|
||
The `extension` keyword declares that you’re about to define one or more extension methods on the parameter that’s put in parentheses. | ||
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. | |
Here, `makeInt` takes a parameter named `radix`. | ||
The code doesn’t account for possible string-to-integer conversion errors, but skipping that detail, the examples show how it works: | ||
|
||
{% tabs extension_2 class=tabs-scala-version %} | ||
{% tab 'Scala 3 only' for=extension_2 %} | ||
benluo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```scala | ||
extension (s: String) | ||
def makeInt(radix: Int): Int = Integer.parseInt(s, radix) | ||
|
@@ -123,14 +177,12 @@ extension (s: String) | |
"10".makeInt(2) // Int = 2 | ||
"100".makeInt(2) // Int = 4 | ||
``` | ||
|
||
|
||
{% endtab %} | ||
{% endtabs %} | ||
|
||
## See also | ||
|
||
Scala Methods can be much more powerful: they can take type parameters and context parameters. | ||
They are covered in detail in the [Domain Modeling][data-1] section. | ||
|
||
|
||
|
||
[data-1]: {% link _overviews/scala3-book/domain-modeling-tools.md %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.