From 30da29723998afedcbe037fa1cf90f5d21a6a74b Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Thu, 6 Oct 2022 11:33:37 +0800 Subject: [PATCH 1/6] add code tabs in num10. --- _overviews/scala3-book/taste-methods.md | 64 ++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/_overviews/scala3-book/taste-methods.md b/_overviews/scala3-book/taste-methods.md index 6f6012d856..44bf25ee93 100644 --- a/_overviews/scala3-book/taste-methods.md +++ b/_overviews/scala3-book/taste-methods.md @@ -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 %} ```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 %} +{% 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 %} ```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 %} From 0153f367c5ab6193c44f0d09d2c19b7564a91776 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:47:38 +0800 Subject: [PATCH 2/6] Update _overviews/scala3-book/taste-methods.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-methods.md b/_overviews/scala3-book/taste-methods.md index 44bf25ee93..819f363bbe 100644 --- a/_overviews/scala3-book/taste-methods.md +++ b/_overviews/scala3-book/taste-methods.md @@ -119,7 +119,7 @@ makeConnection( 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 %} +{% tabs method_9 %} {% tab 'Scala 2 and 3' for=method_9 %} ```scala engage(true, true, true, false) From d2227a9b3477287c9116e6bc0d368031f9bbd168 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:48:03 +0800 Subject: [PATCH 3/6] Update _overviews/scala3-book/taste-methods.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-methods.md b/_overviews/scala3-book/taste-methods.md index 819f363bbe..b4b7d010bb 100644 --- a/_overviews/scala3-book/taste-methods.md +++ b/_overviews/scala3-book/taste-methods.md @@ -168,7 +168,7 @@ 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 %} +{% tab 'Scala 3 Only' for=extension_2 %} ```scala extension (s: String) def makeInt(radix: Int): Int = Integer.parseInt(s, radix) From 5a5021cc5ae7dbec1d7179935fe819069944823c Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:48:10 +0800 Subject: [PATCH 4/6] Update _overviews/scala3-book/taste-methods.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-methods.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-methods.md b/_overviews/scala3-book/taste-methods.md index b4b7d010bb..cfca3c91a6 100644 --- a/_overviews/scala3-book/taste-methods.md +++ b/_overviews/scala3-book/taste-methods.md @@ -148,7 +148,7 @@ _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 %} +{% tab 'Scala 3 Only' for=extension_1 %} ```scala extension (s: String) def hello: String = s"Hello, ${s.capitalize}!" From b202896829f1e457b01e2ed24c709d61498cbb36 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:53:19 +0800 Subject: [PATCH 5/6] correct 2&3 and 3 Only. --- _overviews/scala3-book/taste-methods.md | 44 ++++--------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/_overviews/scala3-book/taste-methods.md b/_overviews/scala3-book/taste-methods.md index cfca3c91a6..51428e1be9 100644 --- a/_overviews/scala3-book/taste-methods.md +++ b/_overviews/scala3-book/taste-methods.md @@ -14,7 +14,7 @@ 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 %} +{% tabs method_1 %} {% tab 'Scala 2 and 3' for=method_1 %} ```scala def methodName(param1: Type1, param2: Type2): ReturnType = @@ -26,7 +26,7 @@ def methodName(param1: Type1, param2: Type2): ReturnType = Here are a few examples: -{% tabs method_2 class=tabs-scala-version %} +{% tabs method_2 %} {% tab 'Scala 2 and 3' for=method_2 %} ```scala def sum(a: Int, b: Int): Int = a + b @@ -37,7 +37,7 @@ def concatenate(s1: String, s2: String): String = s1 + s2 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 %} +{% tabs method_3 %} {% tab 'Scala 2 and 3' for=method_3 %} ```scala def sum(a: Int, b: Int) = a + b @@ -48,7 +48,7 @@ def concatenate(s1: String, s2: String) = s1 + s2 This is how you call those methods: -{% tabs method_4 class=tabs-scala-version %} +{% tabs method_4 %} {% tab 'Scala 2 and 3' for=method_4 %} ```scala val x = sum(1, 2) @@ -83,7 +83,7 @@ def getStackTraceAsString(t: Throwable): String = 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 %} +{% tabs method_6 %} {% tab 'Scala 2 and 3' for=method_6 %} ```scala def makeConnection(url: String, timeout: Int = 5000): Unit = @@ -94,7 +94,7 @@ def makeConnection(url: String, timeout: Int = 5000): Unit = 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 %} +{% tabs method_7 %} {% tab 'Scala 2 and 3' for=method_7 %} ```scala makeConnection("https://localhost") // url=http://localhost, timeout=5000 @@ -105,7 +105,7 @@ makeConnection("https://localhost", 2500) // url=http://localhost, timeout=250 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 %} +{% tabs method_8 %} {% tab 'Scala 2 and 3' for=method_8 %} ```scala makeConnection( @@ -126,36 +126,6 @@ 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, - directionIsSet = true, - picardSaidMakeItSo = true, - 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 %} -```scala -extension (s: String) - def hello: String = s"Hello, ${s.capitalize}!" - def aloha: String = s"Aloha, ${s.capitalize}!" - -"world".hello // "Hello, World!" -"friend".aloha // "Aloha, Friend!" ``` {% endtab %} {% endtabs %} From 80d9f7ceb65b4886d180df37370563b5501f81c9 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sat, 8 Oct 2022 00:11:25 +0800 Subject: [PATCH 6/6] delete tabs --- _overviews/scala3-book/taste-methods.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/_overviews/scala3-book/taste-methods.md b/_overviews/scala3-book/taste-methods.md index 51428e1be9..1c98771ad0 100644 --- a/_overviews/scala3-book/taste-methods.md +++ b/_overviews/scala3-book/taste-methods.md @@ -121,12 +121,11 @@ At a glance, with this method you may wonder which parameters are set to `true` {% tabs method_9 %} {% tab 'Scala 2 and 3' for=method_9 %} + ```scala engage(true, true, true, false) ``` -{% endtab %} -{% endtabs %} -``` + {% endtab %} {% endtabs %} @@ -139,6 +138,7 @@ The code doesn’t account for possible string-to-integer conversion errors, but {% tabs extension_2 class=tabs-scala-version %} {% tab 'Scala 3 Only' for=extension_2 %} + ```scala extension (s: String) def makeInt(radix: Int): Int = Integer.parseInt(s, radix) @@ -147,6 +147,7 @@ extension (s: String) "10".makeInt(2) // Int = 2 "100".makeInt(2) // Int = 4 ``` + {% endtab %} {% endtabs %}