Skip to content

Commit 23d6dda

Browse files
committed
Use scala-cli
1 parent 178372b commit 23d6dda

File tree

5 files changed

+187
-78
lines changed

5 files changed

+187
-78
lines changed

_overviews/getting-started/index.md

Lines changed: 138 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,144 @@ To install them manually:
145145
or [AdoptOpenJDK 8/11](https://adoptopenjdk.net/). Refer to [JDK Compatibility](/overviews/jdk-compatibility/overview.html) for Scala/Java compatibility detail.
146146
1. Install [sbt](https://www.scala-sbt.org/download.html)
147147
148+
## Using the scala-cli command
149+
150+
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+
def hello(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+
//> using dep "com.lihaoyi::os-lib:0.10.7"
201+
202+
@main
203+
def countFiles(): Unit =
204+
val paths = 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+
//> using toolkit default
227+
228+
@main
229+
def countFiles(): Unit =
230+
val paths = 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+
148286
## Create a "Hello World" project with sbt
149287

150288
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:
229367
When you’re finished experimenting with this project, press `[Enter]` to interrupt the `run` command.
230368
Then type `exit` or press `[Ctrl+D]` to exit sbt and return to your command line prompt.
231369

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-
242370
## Getting Help
243371
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.

_overviews/scala-book/two-types-variables.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ object Hello3 extends App {
9494
As before:
9595

9696
- Save that code in a file named *Hello3.scala*
97-
- Compile it with `scalac Hello3.scala`
98-
- Run it with `scala Hello3`
97+
- Compile and run it with `scala-cli run Hello3.scala`
9998

10099

101100

_overviews/scala3-book/methods-main-methods.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ Scala 3 offers a new way to define programs that can be invoked from the command
2222
{% endtab %}
2323
{% endtabs %}
2424

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`:
2626

2727
```bash
28-
$ scala Hello.scala
28+
$ scala-cli run Hello.scala
2929
Hello, World
3030
```
3131

@@ -64,10 +64,10 @@ For example, given this `@main` method that takes an `Int`, a `String`, and a va
6464
{% endtab %}
6565
{% endtabs %}
6666

67-
When you compile that code, it creates a main program named `happyBirthday` that’s called like this:
67+
Pass the arguments after `--`:
6868

6969
```
70-
$ scala happyBirthday 23 Lisa Peter
70+
$ scala-cli run happyBirthday.scala -- 23 Lisa Peter
7171
Happy 23rd Birthday, Lisa and Peter!
7272
```
7373

@@ -79,10 +79,10 @@ The program implemented from an `@main` method checks that there are enough argu
7979
If a check fails, the program is terminated with an error message:
8080

8181
```
82-
$ scala happyBirthday 22
82+
$ scala-cli run happyBirthday.scala -- 22
8383
Illegal command line after first argument: more arguments expected
8484
85-
$ scala happyBirthday sixty Fred
85+
$ scala-cli run happyBirthday.scala -- sixty Fred
8686
Illegal command line: java.lang.NumberFormatException: For input string: "sixty"
8787
```
8888

@@ -176,11 +176,9 @@ object happyBirthday {
176176
{% endtab %}
177177
{% endtabs %}
178178

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:
180180

181181
```bash
182-
$ scalac happyBirthday.scala
183-
184-
$ scala happyBirthday 23 Lisa Peter
182+
$ scala-cli run happyBirthday.scala -- 23 Lisa Peter
185183
Happy 23rd Birthday, Lisa and Peter!
186184
```

_overviews/scala3-book/taste-hello-world.md

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -47,53 +47,27 @@ object hello {
4747
{% endtabs %}
4848
<!-- End tabs -->
4949

50-
Next, compile the code with `scalac`:
50+
Next, compile and run the code with `scala-cli`:
5151

5252
```bash
53-
$ scalac hello.scala
53+
$ scala-cli run hello.scala
5454
```
5555

56-
If you’re coming to Scala from Java, `scalac` is just like `javac`, so that command creates several files:
56+
When you run the command for the first time, two hidden directories will be created: `.bsp` and `.scala-build`. The first
57+
one contains the [Build Server Protocol](https://build-server-protocol.github.io/) information for IDEs, the second one contains the results
58+
of compilation.
5759

58-
<!-- Display Hello World compiled outputs for each Scala Version -->
59-
{% tabs hello-world-outputs class=tabs-scala-version %}
60-
61-
{% tab 'Scala 2' for=hello-world-outputs %}
62-
```bash
63-
$ ls -1
64-
hello$.class
65-
hello.class
66-
hello.scala
60+
The command should produce similar output:
6761
```
68-
{% endtab %}
69-
70-
{% tab 'Scala 3' for=hello-world-outputs %}
71-
```bash
72-
$ ls -1
73-
hello$package$.class
74-
hello$package.class
75-
hello$package.tasty
76-
hello.scala
77-
hello.class
78-
hello.tasty
79-
```
80-
{% endtab %}
81-
82-
{% endtabs %}
83-
<!-- End tabs -->
84-
85-
Like Java, the _.class_ files are bytecode files, and they’re ready to run in the JVM.
86-
87-
Now you can run the `hello` method with the `scala` command:
88-
89-
```bash
90-
$ scala hello
62+
Compiling project (Scala 3.4.2, JVM (20))
63+
Compiled project (Scala 3.4.2, JVM (20))
9164
Hello, World!
9265
```
9366

9467
Assuming that worked, congratulations, you just compiled and ran your first Scala application.
9568

9669
> More information about sbt and other tools that make Scala development easier can be found in the [Scala Tools][scala_tools] chapter.
70+
> The Scala CLI documentation can be found [here](https://scala-cli.virtuslab.org/).
9771
9872
## Ask For User Input
9973

@@ -152,24 +126,23 @@ use the `+` operator on strings to join `"Hello, "` with `name` and `"!"`, makin
152126

153127
> You can learn more about using `val` by reading [Variables and Data Types](/scala3/book/taste-vars-data-types.html).
154128
155-
Then compile the code with `scalac`:
156-
157-
```bash
158-
$ scalac helloInteractive.scala
159-
```
160-
Then run it with `scala helloInteractive`, this time the program will pause after asking for your name,
129+
Then run the code with `scala-cli`. This time the program will pause after asking for your name,
161130
and wait until you type a name and press return on the keyboard, looking like this:
162131

163132
```bash
164-
$ scala helloInteractive
133+
$ scala-cli run helloInteractive.scala
134+
Compiling project (Scala 3.4.2, JVM (20))
135+
Compiled project (Scala 3.4.2, JVM (20))
165136
Please enter your name:
166137
167138
```
168139

169140
When you enter your name at the prompt, the final interaction should look like this:
170141

171142
```bash
172-
$ scala helloInteractive
143+
$ scala-cli run helloInteractive.scala
144+
Compiling project (Scala 3.4.2, JVM (20))
145+
Compiled project (Scala 3.4.2, JVM (20))
173146
Please enter your name:
174147
Alvin Alexander
175148
Hello, Alvin Alexander!

_overviews/tutorials/scala-for-java-programmers.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,38 +160,49 @@ package, so can be accessed from anywhere in a program.
160160

161161
> **Note:** The following assumes you are using Scala on the command line
162162
163+
If we save the above program in a file called
164+
`HelloWorld.scala`, we can run it by issuing the following
165+
command (the greater-than sign `>` represents the shell prompt
166+
and should not be typed):
167+
168+
```shell
169+
> scala-cli run HelloWorld.scala
170+
```
171+
172+
The program will be automatically compiled (with compiled classes somewhere in the newly created `.scala-build` directory)
173+
and executed, producing a similar output:
174+
```
175+
Compiling project (Scala 3.4.2, JVM (20))
176+
Compiled project (Scala 3.4.2, JVM (20))
177+
Hello, World!
178+
```
179+
163180
#### Compiling From the Command Line
164181

165-
To compile the example, we use `scalac`, the Scala compiler. `scalac`
182+
To compile the example, we use `scala-cli compile` command, which will invoke the Scala compiler, `scalac`. `scalac`
166183
works like most compilers: it takes a source file as argument, maybe
167184
some options, and produces one or several output files. The outputs
168185
it produces are standard Java class files.
169186

170-
If we save the above program in a file called
171-
`HelloWorld.scala`, we can compile it by issuing the following
172-
command (the greater-than sign `>` represents the shell prompt
173-
and should not be typed):
174-
175187
```shell
176-
> scalac HelloWorld.scala
188+
> scala-cli compile HelloWorld.scala -d .
177189
```
178190

179-
This will generate a few class files in the current directory. One of
191+
This will generate a few class files in the current directory (`-d .` option sets the compilation output directory). One of
180192
them will be called `HelloWorld.class`, and contains a class
181-
which can be directly executed using the `scala` command, as the
193+
which can be directly executed using the `scala-cli` command, as the
182194
following section shows.
183195

184196
#### Running From the Command Line
185197

186-
Once compiled, a Scala program can be run using the `scala` command.
198+
Once compiled, the program can be run using the `scala-cli run` command.
187199
Its usage is very similar to the `java` command used to run Java
188-
programs, and accepts the same options. The above example can be
200+
programs, and accepts similar options. The above example can be
189201
executed using the following command, which produces the expected
190202
output:
191203

192204
```shell
193-
> scala -classpath . HelloWorld
194-
205+
> scala-cli run --main-class HelloWorld -classpath .
195206
Hello, World!
196207
```
197208

0 commit comments

Comments
 (0)