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
or [AdoptOpenJDK 8/11](https://adoptopenjdk.net/). Refer to [JDK Compatibility](/overviews/jdk-compatibility/overview.html) for Scala/Java compatibility detail.
Create a file named `hello.scala` with following code:
151
+
```scala
152
+
@main
153
+
def hello(): Unit =
154
+
println("Hello, World!")
155
+
```
156
+
157
+
You can define a method with `def` keyword and mark it as a "main" method with the `@main` annotation, designating it as
158
+
the entry point in program execution. The method's type is `Unit`, which means it does not return a value. `Unit`
159
+
can be thought of as an analogue to `void` keyword found in other languages. The `println` method will print the `"Hello World!"`
160
+
string to standard output.
161
+
162
+
To run the program, execute `scala-cli run hello.scala` command. The file will be compiled and executed, with console output
163
+
similar to following:
164
+
```
165
+
$ scala-cli run hello.scala
166
+
Compiling project (Scala 3.4.2, JVM (20))
167
+
Compiled project (Scala 3.4.2, JVM (20))
168
+
Hello, World!
169
+
```
170
+
171
+
### Handling command-line arguments
172
+
173
+
Let's rewrite the `hello.scala` file so that the program greets the person running it.
174
+
```scala
175
+
@main
176
+
defhello(name: String):Unit=
177
+
println(s"Hello, $name!")
178
+
```
179
+
180
+
The `name` argument is expected to be provided when executing the program and if it's not found, the execution will fail.
181
+
The `println` method receives an interpolated string, as indicated by the `s` letter preceding its content. `$name` will be substituted by
182
+
the content of `name` argument.
183
+
184
+
To pass the arguments when executing the program, put them after `--`:
185
+
```
186
+
$ scala-cli run hello.scala -- Gabriel
187
+
Compiling project (Scala 3.4.2, JVM (20))
188
+
Compiled project (Scala 3.4.2, JVM (20))
189
+
Hello, Gabriel!
190
+
```
191
+
192
+
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
+
194
+
### Adding dependencies
195
+
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 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 `//> using` directive. Put the following code in `counter.scala`.
199
+
```scala
200
+
//>usingdep"com.lihaoyi::os-lib:0.10.7"
201
+
202
+
@main
203
+
defcountFiles():Unit=
204
+
valpaths= os.list(os.pwd)
205
+
println(paths.length)
206
+
```
207
+
208
+
In the code above, the `os.pwd` returns current working directory, which then is passed to `os.list`, which returns a sequence
209
+
of paths directly within the directory passed as argument. `val` is used to declare an immutable value, in this example storing the
210
+
sequence of paths.
211
+
212
+
Execute the program. The dependency will be automatically downloaded. The execution should result in a similar output:
213
+
```
214
+
$ scala-cli run counter.scala
215
+
Compiling project (Scala 3.4.2, JVM (20))
216
+
Compiled project (Scala 3.4.2, JVM (20))
217
+
4
218
+
```
219
+
The printed number should be 4: `hello.scala`, `counter.scala` and two hidden directories created automatically when a program is executed:
220
+
`.bsp` containing information about project used by IDEs, and `.scala-build` containing the results of compilation.
221
+
222
+
As it turns out, the `os-lib` library is part of Scala Toolkit, a collection of libraries recommended for tasks like testing,
223
+
operating system interaction or handling JSONs. You can read more about libraries included in the toolkit [here](/toolkit/introduction.html).
224
+
To include the toolkit libraries, use `//> using toolkit default` directive:
225
+
```scala
226
+
//>usingtoolkitdefault
227
+
228
+
@main
229
+
defcountFiles():Unit=
230
+
valpaths= os.list(os.pwd)
231
+
println(paths.length)
232
+
```
233
+
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 toolkit.
236
+
237
+
### Using scala-cli REPL
238
+
239
+
You can execute code interactively using REPL provided by `scala-cli` command. Execute `scala-cli` in console without any arguments.
240
+
```
241
+
$ scala-cli
242
+
Welcome to Scala 3.4.2 (20-ea, Java OpenJDK 64-Bit Server VM).
243
+
Type in expressions for evaluation. Or try :help.
244
+
245
+
scala>
246
+
```
247
+
248
+
Write a line of code to be executed and press enter.
249
+
```
250
+
scala> println("Hello, World!")
251
+
Hello, World!
252
+
253
+
scala>
254
+
```
255
+
256
+
The result will be printed immediately after executing the line. You can declare values:
257
+
```
258
+
scala> val i = 1
259
+
val i: Int = 1
260
+
261
+
scala>
262
+
```
263
+
264
+
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.
265
+
```
266
+
scala> i + 3
267
+
val res0: Int = 4
268
+
269
+
scala>
270
+
```
271
+
You can exit the REPL with `:exit`.
272
+
273
+
### Next steps
274
+
275
+
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
276
+
sbt and an IDE using the tutorials below. If you want to familiarize yourself with the language more, consider checking out:
277
+
278
+
*[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.
279
+
*[The Tour of Scala](/tour/tour-of-scala.html) for bite-sized introductions to Scala's features.
280
+
*[Learning Resources](/learn.html), which includes online interactive tutorials and courses.
281
+
*[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.
285
+
148
286
## Create a "Hello World" project with sbt
149
287
150
288
Once you have installed sbt, you are ready to create a Scala project, which
@@ -229,15 +367,5 @@ Otherwise, you can run the application from a terminal with these steps:
229
367
When you’re finished experimenting with this project, press `[Enter]` to interrupt the `run` command.
230
368
Then type `exit` or press `[Ctrl+D]` to exit sbt and return to your command line prompt.
231
369
232
-
## Next Steps
233
-
234
-
Once you've finished the above tutorials, consider checking out:
235
-
236
-
* [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.
237
-
* [The Tour of Scala](/tour/tour-of-scala.html) for bite-sized introductions to Scala's features.
238
-
* [Learning Resources](/learn.html), which includes online interactive tutorials and courses.
239
-
* [Our list of some popular Scala books](/books.html).
240
-
* [The migration guide](/scala3/guides/migration/compatibility-intro.html) helps you to migrate your existing Scala 2 code base to Scala 3.
241
-
242
370
## Getting Help
243
371
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
+8-10Lines changed: 8 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -22,10 +22,10 @@ Scala 3 offers a new way to define programs that can be invoked from the command
22
22
{% endtab %}
23
23
{% endtabs %}
24
24
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`:
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-cli`:
26
26
27
27
```bash
28
-
$ scala Hello.scala
28
+
$ scala-cli 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
-
$ scalahappyBirthday 23 Lisa Peter
70
+
$ scala-cli 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
-
$ scalahappyBirthday 22
82
+
$ scala-cli run happyBirthday.scala -- 22
83
83
Illegal command line after first argument: more arguments expected
84
84
85
-
$ scalahappyBirthday sixty Fred
85
+
$ scala-cli 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-cli`, as shown previously:
180
180
181
181
```bash
182
-
$ scalac happyBirthday.scala
183
-
184
-
$ scala happyBirthday 23 Lisa Peter
182
+
$ scala-cli run happyBirthday.scala -- 23 Lisa Peter
0 commit comments