Skip to content

Commit 011d86e

Browse files
authored
Merge pull request #2487 from byyue/dev-2481
Add code tabs for scala3-book/taste-repl
2 parents 1d7d425 + f153324 commit 011d86e

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

_overviews/scala3-book/taste-repl.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,74 @@ next-page: taste-vars-data-types
1111
The Scala REPL (“Read-Evaluate-Print-Loop”) is a command-line interpreter that you use as a “playground” area to test your Scala code.
1212
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:
1313

14+
15+
{% tabs command-line class=tabs-scala-version %}
16+
17+
{% tab 'Scala 2' for=command-line %}
1418
```bash
1519
$ scala
16-
Welcome to Scala 3.0.0 (OpenJDK 64-Bit Server VM, Java 11.0.9).
17-
Type in expressions for evaluation.
18-
Or try :help.
20+
Welcome to Scala {{site.scala-version}} (OpenJDK 64-Bit Server VM, Java 1.8.0_342).
21+
Type in expressions for evaluation. Or try :help.
1922

2023
scala> _
2124
```
25+
{% endtab %}
26+
27+
{% tab 'Scala 3' for=command-line %}
28+
```bash
29+
$ scala
30+
Welcome to Scala {{site.scala-3-version}} (1.8.0_322, Java OpenJDK 64-Bit Server VM).
31+
Type in expressions for evaluation. Or try :help.
32+
33+
scala> _
34+
```
35+
{% endtab %}
36+
37+
{% endtabs %}
2238

2339
The REPL is a command-line interpreter, so it sits there waiting for you to type something.
2440
Now you can type Scala expressions to see how they work:
2541

42+
{% tabs expression-one %}
43+
{% tab 'Scala 2 and 3' for=expression-one %}
2644
````
2745
scala> 1 + 1
2846
val res0: Int = 2
2947
3048
scala> 2 + 2
3149
val res1: Int = 4
3250
````
51+
{% endtab %}
52+
{% endtabs %}
3353

3454
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.
3555
You can use these variable names in subsequent expressions:
3656

57+
{% tabs expression-two %}
58+
{% tab 'Scala 2 and 3' for=expression-two %}
3759
````
3860
scala> val x = res0 * 10
3961
val x: Int = 20
4062
````
63+
{% endtab %}
64+
{% endtabs %}
4165

4266
Notice that the REPL output also shows the result of your expressions.
4367

4468
You can run all sorts of experiments in the REPL.
4569
This example shows how to create and then call a `sum` method:
4670

71+
{% tabs expression-three %}
72+
{% tab 'Scala 2 and 3' for=expression-three %}
4773
````
4874
scala> def sum(a: Int, b: Int): Int = a + b
4975
def sum(a: Int, b: Int): Int
5076
5177
scala> sum(2, 2)
5278
val res2: Int = 4
5379
````
80+
{% endtab %}
81+
{% endtabs %}
5482

5583
If you prefer a browser-based playground environment, you can also use [scastie.scala-lang.org](https://scastie.scala-lang.org).
5684

0 commit comments

Comments
 (0)