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
Copy file name to clipboardExpand all lines: _overviews/getting-started/index.md
+31-28
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,8 @@ Run the following command in your terminal, following the on-screen instructions
100
100
Check your setup with the command `scala -version`, which should output:
101
101
```bash
102
102
$ scala -version
103
-
Scala code runner version {{site.scala-3-version}} -- Copyright 2002-2022, LAMP/EPFL
103
+
Scala code runner version: 1.4.3
104
+
Scala version (default): {{site.scala-3-version}}
104
105
```
105
106
{% endaltDetails %}
106
107
<!-- end Alternative Detail -->
@@ -147,8 +148,10 @@ To install them manually:
147
148
148
149
## Using the Scala CLI
149
150
150
-
Create a file named `hello.scala` with the following code:
151
+
In a directory of your choice, which we will call `<project-dir>`, create a file named `hello.scala` with the following code:
151
152
```scala
153
+
//> using scala {{site.scala-3-version}}
154
+
152
155
@main
153
156
def hello(): Unit =
154
157
println("Hello, World!")
@@ -159,19 +162,21 @@ the entry point in program execution. The method's type is `Unit`, which means i
159
162
can be thought of as an analogue to the `void` keyword found in other languages. The `println` method will print the `"Hello, World!"`
160
163
string to standard output.
161
164
162
-
To run the program, execute `scala run hello.scala` command. The file will be compiled and executed, with console output
165
+
To run the program, execute `scala run hello.scala` command from a terminal, within the `<project-dir>` directory. The file will be compiled and executed, with console output
You can read more about [main methods](/scala3/book/methods-main-methods.html) and [string interpolation](/scala3/book/string-interpolation.html) in the Scala Book.
193
198
194
199
### Adding dependencies
195
200
196
-
Let's write a program that will count the files and directories present in its working directory. While in Scala you have full access to the Java API for
197
-
filesystem interaction, the [os-lib](https://github.com/com-lihaoyi/os-lib) library by Li Haoyi is much more convenient to use. A dependency on the library can
198
-
be added with the `//> using` directive. Put the following code in `counter.scala`.
201
+
We now write a program that will count the files and directories present in its working directory.
202
+
We use the [os-lib](https://github.com/com-lihaoyi/os-lib) library from the [Scala toolkit](toolkit/introduction.html)
203
+
for that purpose. A dependency on the library can be added with the `//> using` directive. Put the following code in `counter.scala`.
199
204
```scala
205
+
//>usingscala{{site.scala-3-version}}
200
206
//>usingdep"com.lihaoyi::os-lib:0.10.7"
201
207
202
208
@main
203
209
defcountFiles():Unit=
204
-
valpaths= os.list(os.pwd)
205
-
println(paths.length)
210
+
valpaths= os.list(os.pwd)
211
+
println(paths.length)
206
212
```
207
213
208
-
In the code above, the `os.pwd` returns the current working directory, which is then passed to `os.list`, which returns a sequence
209
-
of paths directly within the directory passed as an argument. `val` is used to declare an immutable value, in this example storing the
214
+
In the code above, `os.pwd` returns the current working directory. We pass it to `os.list`, which returns a sequence
215
+
of paths directly within the directory passed as an argument. We use a `val` to declare an immutable value, in this example storing the
210
216
sequence of paths.
211
217
212
218
Execute the program. The dependency will be automatically downloaded. The execution should result in a similar output:
The printed number should be 4: `hello.scala`, `counter.scala` and two hidden directories created automatically when a program is executed:
@@ -223,23 +229,23 @@ As it turns out, the `os-lib` library is a part of Scala Toolkit, a collection o
223
229
operating system interaction or handling JSONs. You can read more about the libraries included in the toolkit [here](/toolkit/introduction.html).
224
230
To include the toolkit libraries, use the `//> using toolkit default` directive:
225
231
```scala
226
-
//>usingtoolkitdefault
232
+
//>usingscala{{site.scala-3-version}}
233
+
//>usingtoolkit0.5.0
227
234
228
235
@main
229
236
defcountFiles():Unit=
230
-
valpaths= os.list(os.pwd)
231
-
println(paths.length)
237
+
valpaths= os.list(os.pwd)
238
+
println(paths.length)
232
239
```
233
240
234
-
This program is identical to the one above, with the only difference being that other toolkit libraries will also be available to use
235
-
and their downloaded versions, instead of being specified by hand, will be the newest ones included in the toolkit.
241
+
This program is identical to the one above. However, other toolkit libraries will also be available to use, should you need them.
236
242
237
-
### Using REPL
243
+
### Using the REPL
238
244
239
-
You can execute code interactively using REPL provided by the `scala` command. Execute `scala` in console without any arguments.
245
+
You can execute code interactively using the REPL provided by the `scala` command. Execute `scala` in the console without any arguments.
240
246
```
241
247
$ scala
242
-
Welcome to Scala 3.5.0 (20-ea, Java OpenJDK 64-Bit Server VM).
248
+
Welcome to Scala {{site.scala-3-version}} (20-ea, Java OpenJDK 64-Bit Server VM).
243
249
Type in expressions for evaluation. Or try :help.
244
250
245
251
scala>
@@ -279,9 +285,6 @@ sbt and an IDE using the tutorials below. If you want to familiarize yourself wi
279
285
*[The Tour of Scala](/tour/tour-of-scala.html) for bite-sized introductions to Scala's features.
280
286
*[Learning Resources](/learn.html), which includes online interactive tutorials and courses.
281
287
*[Our list of some popular Scala books](/books.html).
282
-
*[The migration guide](/scala3/guides/migration/compatibility-intro.html) helps you to migrate your existing Scala 2 code base to Scala 3.
283
-
284
-
The [Scala CLI documentation](https://scala-cli.virtuslab.org/) describes the available sub-commands and how to integrate the tool with an IDE of choice.
0 commit comments