From 67b0c934ac92b6e566928d6a74208c1837ccc5bd Mon Sep 17 00:00:00 2001 From: Brian Yue Date: Sat, 6 Aug 2022 01:45:55 +0800 Subject: [PATCH 1/4] Add code tabs for scala3-book/taste-repl --- _overviews/scala3-book/taste-repl.md | 34 +++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/_overviews/scala3-book/taste-repl.md b/_overviews/scala3-book/taste-repl.md index 50820b473f..ff2fa9743e 100644 --- a/_overviews/scala3-book/taste-repl.md +++ b/_overviews/scala3-book/taste-repl.md @@ -11,18 +11,36 @@ next-page: taste-vars-data-types The Scala REPL (“Read-Evaluate-Print-Loop”) is a command-line interpreter that you use as a “playground” area to test your Scala code. You start a REPL session by running the `scala` or `scala3` command depending on your installation at your operating system command line, where you’ll see a “welcome” prompt like this: + +{% tabs command-line class=tabs-scala-version %} + +{% tab 'Scala 3' for=command-line %} ```bash $ scala -Welcome to Scala 3.0.0 (OpenJDK 64-Bit Server VM, Java 11.0.9). -Type in expressions for evaluation. -Or try :help. +Welcome to Scala 3.1.3 (1.8.0_322, Java OpenJDK 64-Bit Server VM). +Type in expressions for evaluation. Or try :help. scala> _ ``` +{% endtab %} + +{% tab 'Scala 2' for=command-line %} +```bash +$ scala +Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_342). +Type in expressions for evaluation. Or try :help. + +scala> _ +``` +{% endtab %} + +{% endtabs %} The REPL is a command-line interpreter, so it sits there waiting for you to type something. Now you can type Scala expressions to see how they work: +{% tabs expression-one %} +{% tab 'Scala 2 and 3' for=expression-one %} ```` scala> 1 + 1 val res0: Int = 2 @@ -30,20 +48,28 @@ val res0: Int = 2 scala> 2 + 2 val res1: Int = 4 ```` +{% endtab %} +{% endtabs %} As shown in the output, if you don’t assign a variable to the result of an expression, the REPL creates variables named `res0`, `res1`, etc., for you. You can use these variable names in subsequent expressions: +{% tabs expression-two %} +{% tab 'Scala 2 and 3' for=expression-two %} ```` scala> val x = res0 * 10 val x: Int = 20 ```` +{% endtab %} +{% endtabs %} Notice that the REPL output also shows the result of your expressions. You can run all sorts of experiments in the REPL. This example shows how to create and then call a `sum` method: +{% tabs expression-three %} +{% tab 'Scala 2 and 3' for=expression-three %} ```` scala> def sum(a: Int, b: Int): Int = a + b def sum(a: Int, b: Int): Int @@ -51,6 +77,8 @@ def sum(a: Int, b: Int): Int scala> sum(2, 2) val res2: Int = 4 ```` +{% endtab %} +{% endtabs %} If you prefer a browser-based playground environment, you can also use [scastie.scala-lang.org](https://scastie.scala-lang.org). From 2a87477ef09c2771f4eabdb1cd701f4e23be73e1 Mon Sep 17 00:00:00 2001 From: Brian Yue <46839413+byyue@users.noreply.github.com> Date: Sat, 6 Aug 2022 12:02:15 +0800 Subject: [PATCH 2/4] Update _overviews/scala3-book/taste-repl.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-repl.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-repl.md b/_overviews/scala3-book/taste-repl.md index ff2fa9743e..3ed005ba55 100644 --- a/_overviews/scala3-book/taste-repl.md +++ b/_overviews/scala3-book/taste-repl.md @@ -17,7 +17,7 @@ You start a REPL session by running the `scala` or `scala3` command depending on {% tab 'Scala 3' for=command-line %} ```bash $ scala -Welcome to Scala 3.1.3 (1.8.0_322, Java OpenJDK 64-Bit Server VM). +Welcome to Scala {{site.scala-3-version}} (1.8.0_322, Java OpenJDK 64-Bit Server VM). Type in expressions for evaluation. Or try :help. scala> _ From 04b4fcb1bc9009881aeedd7eaa6fcbd7d09e3a3d Mon Sep 17 00:00:00 2001 From: Brian Yue <46839413+byyue@users.noreply.github.com> Date: Sat, 6 Aug 2022 12:02:20 +0800 Subject: [PATCH 3/4] Update _overviews/scala3-book/taste-repl.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-repl.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-repl.md b/_overviews/scala3-book/taste-repl.md index 3ed005ba55..5f5c03f46e 100644 --- a/_overviews/scala3-book/taste-repl.md +++ b/_overviews/scala3-book/taste-repl.md @@ -27,7 +27,7 @@ scala> _ {% tab 'Scala 2' for=command-line %} ```bash $ scala -Welcome to Scala 2.13.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_342). +Welcome to Scala {{site.scala-version}} (OpenJDK 64-Bit Server VM, Java 1.8.0_342). Type in expressions for evaluation. Or try :help. scala> _ From f153324d13ae792d2be677a8b4ca91fa3bec4e97 Mon Sep 17 00:00:00 2001 From: Brian Yue Date: Tue, 9 Aug 2022 18:53:22 +0800 Subject: [PATCH 4/4] Put Scala 2 tab before Scala 3 --- _overviews/scala3-book/taste-repl.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_overviews/scala3-book/taste-repl.md b/_overviews/scala3-book/taste-repl.md index 5f5c03f46e..20eecec556 100644 --- a/_overviews/scala3-book/taste-repl.md +++ b/_overviews/scala3-book/taste-repl.md @@ -14,20 +14,20 @@ You start a REPL session by running the `scala` or `scala3` command depending on {% tabs command-line class=tabs-scala-version %} -{% tab 'Scala 3' for=command-line %} +{% tab 'Scala 2' for=command-line %} ```bash $ scala -Welcome to Scala {{site.scala-3-version}} (1.8.0_322, Java OpenJDK 64-Bit Server VM). +Welcome to Scala {{site.scala-version}} (OpenJDK 64-Bit Server VM, Java 1.8.0_342). Type in expressions for evaluation. Or try :help. scala> _ ``` {% endtab %} -{% tab 'Scala 2' for=command-line %} +{% tab 'Scala 3' for=command-line %} ```bash $ scala -Welcome to Scala {{site.scala-version}} (OpenJDK 64-Bit Server VM, Java 1.8.0_342). +Welcome to Scala {{site.scala-3-version}} (1.8.0_322, Java OpenJDK 64-Bit Server VM). Type in expressions for evaluation. Or try :help. scala> _