You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Scala REPL (“Read-Evaluate-Print-Loop”) is a command-line interpreter that you use as a “playground” area to test your Scala code.
12
12
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:
13
13
14
+
15
+
{% tabs command-line class=tabs-scala-version %}
16
+
17
+
{% tab 'Scala 2' for=command-line %}
14
18
```bash
15
19
$ 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.
19
22
20
23
scala> _
21
24
```
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 %}
22
38
23
39
The REPL is a command-line interpreter, so it sits there waiting for you to type something.
24
40
Now you can type Scala expressions to see how they work:
25
41
42
+
{% tabs expression-one %}
43
+
{% tab 'Scala 2 and 3' for=expression-one %}
26
44
````
27
45
scala> 1 + 1
28
46
val res0: Int = 2
29
47
30
48
scala> 2 + 2
31
49
val res1: Int = 4
32
50
````
51
+
{% endtab %}
52
+
{% endtabs %}
33
53
34
54
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.
35
55
You can use these variable names in subsequent expressions:
36
56
57
+
{% tabs expression-two %}
58
+
{% tab 'Scala 2 and 3' for=expression-two %}
37
59
````
38
60
scala> val x = res0 * 10
39
61
val x: Int = 20
40
62
````
63
+
{% endtab %}
64
+
{% endtabs %}
41
65
42
66
Notice that the REPL output also shows the result of your expressions.
43
67
44
68
You can run all sorts of experiments in the REPL.
45
69
This example shows how to create and then call a `sum` method:
46
70
71
+
{% tabs expression-three %}
72
+
{% tab 'Scala 2 and 3' for=expression-three %}
47
73
````
48
74
scala> def sum(a: Int, b: Int): Int = a + b
49
75
def sum(a: Int, b: Int): Int
50
76
51
77
scala> sum(2, 2)
52
78
val res2: Int = 4
53
79
````
80
+
{% endtab %}
81
+
{% endtabs %}
54
82
55
83
If you prefer a browser-based playground environment, you can also use [scastie.scala-lang.org](https://scastie.scala-lang.org).
0 commit comments