Skip to content

Commit 74b36ac

Browse files
authored
Merge pull request #3072 from gkepka/getting-started
Use scala-cli in Getting Started page
2 parents 15b3f6f + 57efc0e commit 74b36ac

File tree

8 files changed

+192
-82
lines changed

8 files changed

+192
-82
lines changed

Diff for: _overviews/getting-started/install-scala.md

+142-11
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ Run the following command in your terminal, following the on-screen instructions
100100
Check your setup with the command `scala -version`, which should output:
101101
```bash
102102
$ 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}}
104105
```
105106
{% endaltDetails %}
106107
<!-- end Alternative Detail -->
@@ -144,6 +145,146 @@ To install them manually:
144145
or [AdoptOpenJDK 8/11](https://adoptopenjdk.net/). Refer to [JDK Compatibility](/overviews/jdk-compatibility/overview.html) for Scala/Java compatibility detail.
145146
1. Install [sbt](https://www.scala-sbt.org/download.html)
146147
148+
## Using the Scala CLI
149+
150+
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
165+
similar to following:
166+
```
167+
$ scala run hello.scala
168+
Compiling project (Scala {{site.scala-3-version}}, JVM (20))
169+
Compiled project (Scala {{site.scala-3-version}}, JVM (20))
170+
Hello, World!
171+
```
172+
173+
### Handling command-line arguments
174+
175+
Rewrite the `hello.scala` file so that the program greets the person running it.
176+
```scala
177+
//> using scala {{site.scala-3-version}}
178+
179+
@main
180+
def hello(name: String): Unit =
181+
println(s"Hello, $name!")
182+
```
183+
184+
The `name` argument is expected to be provided when executing the program, and if it's not found, the execution will fail.
185+
The `println` method receives an interpolated string, as indicated by the `s` letter preceding its content. `$name` will be substituted by
186+
the content of the `name` argument.
187+
188+
To pass the arguments when executing the program, put them after `--`:
189+
```
190+
$ scala run hello.scala -- Gabriel
191+
Compiling project (Scala {{site.scala-3-version}}, JVM (20))
192+
Compiled project (Scala {{site.scala-3-version}}, JVM (20))
193+
Hello, Gabriel!
194+
```
195+
196+
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+
//> using scala {{site.scala-3-version}}
205+
//> using dep "com.lihaoyi::os-lib:0.10.7"
206+
207+
@main
208+
def countFiles(): Unit =
209+
val paths = 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:
218+
```
219+
$ scala run counter.scala
220+
Compiling project (Scala {{site.scala-3-version}}, JVM (20))
221+
Compiled project (Scala {{site.scala-3-version}}, JVM (20))
222+
4
223+
```
224+
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+
//> using scala {{site.scala-3-version}}
232+
//> using toolkit 0.5.0
233+
234+
@main
235+
def countFiles(): Unit =
236+
val paths = 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+
147288
## Create a "Hello World" project with sbt
148289

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

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-
240371
## Getting Help
241372
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.

Diff for: _overviews/scala-book/two-types-variables.md

+1-2
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 run Hello3.scala`
9998

10099

101100

Diff for: _overviews/scala3-book/methods-main-methods.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Scala 3 offers a new way to define programs that can be invoked from the command
2525
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`:
2626

2727
```bash
28-
$ scala Hello.scala
28+
$ scala 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 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 run happyBirthday.scala -- 22
8383
Illegal command line after first argument: more arguments expected
8484
85-
$ scala happyBirthday sixty Fred
85+
$ scala 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`, as shown previously:
180180

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

Diff for: _overviews/scala3-book/taste-hello-world.md

+13-44
Original file line numberDiff line numberDiff line change
@@ -47,53 +47,23 @@ object hello {
4747
{% endtabs %}
4848
<!-- End tabs -->
4949

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

5252
```bash
53-
$ scalac hello.scala
53+
$ scala 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:
57-
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
56+
The command should produce an output similar to:
6757
```
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
58+
Compiling project (Scala {{site.scala-3-version}}, JVM (20))
59+
Compiled project (Scala {{site.scala-3-version}}, JVM (20))
9160
Hello, World!
9261
```
9362

9463
Assuming that worked, congratulations, you just compiled and ran your first Scala application.
9564

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

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

153123
> You can learn more about using `val` by reading [Variables and Data Types](/scala3/book/taste-vars-data-types.html).
154124
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,
125+
Then run the code with `scala`. This time the program will pause after asking for your name,
161126
and wait until you type a name and press return on the keyboard, looking like this:
162127

163128
```bash
164-
$ scala helloInteractive
129+
$ scala run helloInteractive.scala
130+
Compiling project (Scala {{site.scala-3-version}}, JVM (20))
131+
Compiled project (Scala {{site.scala-3-version}}, JVM (20))
165132
Please enter your name:
166133
167134
```
168135

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

171138
```bash
172-
$ scala helloInteractive
139+
$ scala run helloInteractive.scala
140+
Compiling project (Scala {{site.scala-3-version}}, JVM (20))
141+
Compiled project (Scala {{site.scala-3-version}}, JVM (20))
173142
Please enter your name:
174143
Alvin Alexander
175144
Hello, Alvin Alexander!

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

+23-12
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 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 an output similar to:
174+
```
175+
Compiling project (Scala {{site.scala-3-version}}, JVM (20))
176+
Compiled project (Scala {{site.scala-3-version}}, 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 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 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
181193
which can be directly executed using the `scala` 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 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 run --main-class HelloWorld -classpath .
195206
Hello, World!
196207
```
197208

Diff for: _ru/getting-started/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ newcomer_resources:
9090
Проверьте корректность установки с помощью команды `scala -version`, которая должна вывести:
9191
```bash
9292
$ scala -version
93-
Scala code runner version {{site.scala-3-version}} -- Copyright 2002-2022, LAMP/EPFL
93+
Scala code runner version: 1.4.3
94+
Scala version (default): {{site.scala-3-version}}
9495
```
9596
Если сообщение не выдано, возможно, необходимо перезайти в терминал (или перезагрузиться),
9697
чтобы изменения вступили в силу.

0 commit comments

Comments
 (0)