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/install-scala.md
+142-11
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 -->
@@ -144,6 +145,146 @@ To install them manually:
144
145
or [AdoptOpenJDK 8/11](https://adoptopenjdk.net/). Refer to [JDK Compatibility](/overviews/jdk-compatibility/overview.html) for Scala/Java compatibility detail.
In a directory of your choice, which we will call `<project-dir>`, create a file named `hello.scala` with the following code:
151
+
```scala
152
+
//> using scala {{site.scala-3-version}}
153
+
154
+
@main
155
+
def hello(): Unit =
156
+
println("Hello, World!")
157
+
```
158
+
159
+
You can define a method with the `def` keyword and mark it as a "main" method with the `@main` annotation, designating it as
160
+
the entry point in program execution. The method's type is `Unit`, which means it does not return a value. `Unit`
161
+
can be thought of as an analogue to the `void` keyword found in other languages. The `println` method will print the `"Hello, World!"`
162
+
string to standard output.
163
+
164
+
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.
197
+
198
+
### Adding dependencies
199
+
200
+
We now write a program that will count the files and directories present in its working directory.
201
+
We use the [os-lib](https://github.com/com-lihaoyi/os-lib) library from the [Scala toolkit](toolkit/introduction.html)
202
+
for that purpose. A dependency on the library can be added with the `//> using` directive. Put the following code in `counter.scala`.
203
+
```scala
204
+
//>usingscala{{site.scala-3-version}}
205
+
//>usingdep"com.lihaoyi::os-lib:0.10.7"
206
+
207
+
@main
208
+
defcountFiles():Unit=
209
+
valpaths= os.list(os.pwd)
210
+
println(paths.length)
211
+
```
212
+
213
+
In the code above, `os.pwd` returns the current working directory. We pass it to `os.list`, which returns a sequence
214
+
of paths directly within the directory passed as an argument. We use a `val` to declare an immutable value, in this example storing the
215
+
sequence of paths.
216
+
217
+
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:
225
+
`.bsp` containing information about project used by IDEs, and `.scala-build` containing the results of compilation.
226
+
227
+
As it turns out, the `os-lib` library is a part of Scala Toolkit, a collection of libraries recommended for tasks like testing,
228
+
operating system interaction or handling JSONs. You can read more about the libraries included in the toolkit [here](/toolkit/introduction.html).
229
+
To include the toolkit libraries, use the `//> using toolkit 0.5.0` directive:
230
+
```scala
231
+
//>usingscala{{site.scala-3-version}}
232
+
//>usingtoolkit0.5.0
233
+
234
+
@main
235
+
defcountFiles():Unit=
236
+
valpaths= os.list(os.pwd)
237
+
println(paths.length)
238
+
```
239
+
240
+
This program is identical to the one above. However, other toolkit libraries will also be available to use, should you need them.
241
+
242
+
### Using the REPL
243
+
244
+
You can execute code interactively using the REPL provided by the `scala` command. Execute `scala` in the console without any arguments.
245
+
```
246
+
$ scala
247
+
Welcome to Scala {{site.scala-3-version}} (20-ea, Java OpenJDK 64-Bit Server VM).
248
+
Type in expressions for evaluation. Or try :help.
249
+
250
+
scala>
251
+
```
252
+
253
+
Write a line of code to be executed and press enter.
254
+
```
255
+
scala> println("Hello, World!")
256
+
Hello, World!
257
+
258
+
scala>
259
+
```
260
+
261
+
The result will be printed immediately after executing the line. You can declare values:
262
+
```
263
+
scala> val i = 1
264
+
val i: Int = 1
265
+
266
+
scala>
267
+
```
268
+
269
+
A new value of type `Int` has been created. If you provide an expression that can be evaluated, its result will be stored in an automatically created value.
270
+
```
271
+
scala> i + 3
272
+
val res0: Int = 4
273
+
274
+
scala>
275
+
```
276
+
You can exit the REPL with `:exit`.
277
+
278
+
### Next steps
279
+
280
+
Now that you have tasted a little bit of Scala, you can either explore the language itself, or learn how to set up a project using the
281
+
sbt and an IDE using the tutorials below. If you want to familiarize yourself with the language more, consider checking out:
282
+
283
+
*[The Scala Book](/scala3/book/introduction.html) (see the Scala 2 version [here](/overviews/scala-book/introduction.html)), which provides a set of short lessons introducing Scala’s main features.
284
+
*[The Tour of Scala](/tour/tour-of-scala.html) for bite-sized introductions to Scala's features.
285
+
*[Learning Resources](/learn.html), which includes online interactive tutorials and courses.
286
+
*[Our list of some popular Scala books](/books.html).
287
+
147
288
## Create a "Hello World" project with sbt
148
289
149
290
Once you have installed sbt, you are ready to create a Scala project, which
@@ -227,15 +368,5 @@ Otherwise, you can run the application from a terminal with these steps:
227
368
When you’re finished experimenting with this project, press `[Enter]` to interrupt the `run` command.
228
369
Then type `exit` or press `[Ctrl+D]` to exit sbt and return to your command line prompt.
229
370
230
-
## Next Steps
231
-
232
-
Once you've finished the above tutorials, consider checking out:
233
-
234
-
* [The Scala Book](/scala3/book/introduction.html) (see the Scala 2 version [here](/overviews/scala-book/introduction.html)), which provides a set of short lessons introducing Scala’s main features.
235
-
* [The Tour of Scala](/tour/tour-of-scala.html) for bite-sized introductions to Scala's features.
236
-
* [Learning Resources](/learn.html), which includes online interactive tutorials and courses.
237
-
* [Our list of some popular Scala books](/books.html).
238
-
* [The migration guide](/scala3/guides/migration/compatibility-intro.html) helps you to migrate your existing Scala 2 code base to Scala 3.
239
-
240
371
## Getting Help
241
372
There are a multitude of mailing lists and real-time chat rooms in case you want to quickly connect with other Scala users. Check out our [community](https://scala-lang.org/community/) page for a list of these resources, and for where to reach out for help.
Copy file name to clipboardExpand all lines: _overviews/scala3-book/methods-main-methods.md
+7-9
Original file line number
Diff line number
Diff line change
@@ -25,7 +25,7 @@ Scala 3 offers a new way to define programs that can be invoked from the command
25
25
To run this program, save the line of code in a file named as e.g. *Hello.scala*---the filename doesn’t have to match the method name---and run it with `scala`:
26
26
27
27
```bash
28
-
$ scala Hello.scala
28
+
$ scala run Hello.scala
29
29
Hello, World
30
30
```
31
31
@@ -64,10 +64,10 @@ For example, given this `@main` method that takes an `Int`, a `String`, and a va
64
64
{% endtab %}
65
65
{% endtabs %}
66
66
67
-
When you compile that code, it creates a main program named `happyBirthday` that’s called like this:
67
+
Pass the arguments after `--`:
68
68
69
69
```
70
-
$ scala happyBirthday 23 Lisa Peter
70
+
$ scala run happyBirthday.scala -- 23 Lisa Peter
71
71
Happy 23rd Birthday, Lisa and Peter!
72
72
```
73
73
@@ -79,10 +79,10 @@ The program implemented from an `@main` method checks that there are enough argu
79
79
If a check fails, the program is terminated with an error message:
80
80
81
81
```
82
-
$ scala happyBirthday 22
82
+
$ scala run happyBirthday.scala -- 22
83
83
Illegal command line after first argument: more arguments expected
84
84
85
-
$ scala happyBirthday sixty Fred
85
+
$ scala run happyBirthday.scala -- sixty Fred
86
86
Illegal command line: java.lang.NumberFormatException: For input string: "sixty"
87
87
```
88
88
@@ -176,11 +176,9 @@ object happyBirthday {
176
176
{% endtab %}
177
177
{% endtabs %}
178
178
179
-
If you place that code in a file named *happyBirthday.scala*, you can then compile it with `scalac`and run it with `scala`, as shown previously:
179
+
If you place that code in a file named *happyBirthday.scala*, you can then compile and run it with `scala`, as shown previously:
0 commit comments