-
Notifications
You must be signed in to change notification settings - Fork 1k
Use scala-cli in Getting Started page #3072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
23d6dda
Use scala-cli
gkepka 0ff5ba0
Change scala-cli to scala
gkepka 0959275
Link and grammar fixes
gkepka a85e51e
Grammar fixes + Scala 3 version update
gkepka 8e84db6
Use toolkit 0.5.0 version
gkepka 57efc0e
Merge branch 'main' into getting-started
gkepka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Check your setup with the command `scala -version`, which should output: | ||
```bash | ||
$ scala -version | ||
Scala code runner version {{site.scala-3-version}} -- Copyright 2002-2022, LAMP/EPFL | ||
Scala code runner version: 1.4.3 | ||
Scala version (default): {{site.scala-3-version}} | ||
``` | ||
{% endaltDetails %} | ||
<!-- end Alternative Detail --> | ||
|
@@ -147,8 +148,10 @@ To install them manually: | |
|
||
## Using the Scala CLI | ||
|
||
Create a file named `hello.scala` with the following code: | ||
In a directory of your choice, which we will call `<project-dir>`, create a file named `hello.scala` with the following code: | ||
```scala | ||
//> using scala {{site.scala-3-version}} | ||
|
||
@main | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a |
||
def hello(): Unit = | ||
println("Hello, World!") | ||
|
@@ -159,19 +162,21 @@ the entry point in program execution. The method's type is `Unit`, which means i | |
can be thought of as an analogue to the `void` keyword found in other languages. The `println` method will print the `"Hello, World!"` | ||
string to standard output. | ||
|
||
To run the program, execute `scala run hello.scala` command. The file will be compiled and executed, with console output | ||
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 | ||
similar to following: | ||
``` | ||
$ scala run hello.scala | ||
Compiling project (Scala 3.5.0, JVM (20)) | ||
Compiled project (Scala 3.5.0, JVM (20)) | ||
Compiling project (Scala {{site.scala-3-version}}, JVM (20)) | ||
Compiled project (Scala {{site.scala-3-version}}, JVM (20)) | ||
Hello, World! | ||
``` | ||
|
||
### Handling command-line arguments | ||
|
||
Let's rewrite the `hello.scala` file so that the program greets the person running it. | ||
Rewrite the `hello.scala` file so that the program greets the person running it. | ||
```scala | ||
//> using scala {{site.scala-3-version}} | ||
|
||
@main | ||
def hello(name: String): Unit = | ||
println(s"Hello, $name!") | ||
|
@@ -184,36 +189,37 @@ the content of the `name` argument. | |
To pass the arguments when executing the program, put them after `--`: | ||
``` | ||
$ scala run hello.scala -- Gabriel | ||
Compiling project (Scala 3.5.0, JVM (20)) | ||
Compiled project (Scala 3.5.0, JVM (20)) | ||
Compiling project (Scala {{site.scala-3-version}}, JVM (20)) | ||
Compiled project (Scala {{site.scala-3-version}}, JVM (20)) | ||
Hello, Gabriel! | ||
``` | ||
|
||
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. | ||
|
||
### Adding dependencies | ||
|
||
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 | ||
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 | ||
be added with the `//> using` directive. Put the following code in `counter.scala`. | ||
We now write a program that will count the files and directories present in its working directory. | ||
We use the [os-lib](https://github.com/com-lihaoyi/os-lib) library from the [Scala toolkit](toolkit/introduction.html) | ||
for that purpose. A dependency on the library can be added with the `//> using` directive. Put the following code in `counter.scala`. | ||
```scala | ||
//> using scala {{site.scala-3-version}} | ||
//> using dep "com.lihaoyi::os-lib:0.10.7" | ||
|
||
@main | ||
def countFiles(): Unit = | ||
val paths = os.list(os.pwd) | ||
println(paths.length) | ||
val paths = os.list(os.pwd) | ||
println(paths.length) | ||
``` | ||
|
||
In the code above, the `os.pwd` returns the current working directory, which is then passed to `os.list`, which returns a sequence | ||
of paths directly within the directory passed as an argument. `val` is used to declare an immutable value, in this example storing the | ||
In the code above, `os.pwd` returns the current working directory. We pass it to `os.list`, which returns a sequence | ||
of paths directly within the directory passed as an argument. We use a `val` to declare an immutable value, in this example storing the | ||
sequence of paths. | ||
|
||
Execute the program. The dependency will be automatically downloaded. The execution should result in a similar output: | ||
``` | ||
$ scala run counter.scala | ||
Compiling project (Scala 3.5.0, JVM (20)) | ||
Compiled project (Scala 3.5.0, JVM (20)) | ||
Compiling project (Scala {{site.scala-3-version}}, JVM (20)) | ||
Compiled project (Scala {{site.scala-3-version}}, JVM (20)) | ||
4 | ||
``` | ||
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 | |
operating system interaction or handling JSONs. You can read more about the libraries included in the toolkit [here](/toolkit/introduction.html). | ||
To include the toolkit libraries, use the `//> using toolkit default` directive: | ||
gkepka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```scala | ||
//> using toolkit default | ||
//> using scala {{site.scala-3-version}} | ||
//> using toolkit 0.5.0 | ||
|
||
@main | ||
def countFiles(): Unit = | ||
val paths = os.list(os.pwd) | ||
println(paths.length) | ||
val paths = os.list(os.pwd) | ||
println(paths.length) | ||
``` | ||
|
||
This program is identical to the one above, with the only difference being that other toolkit libraries will also be available to use | ||
and their downloaded versions, instead of being specified by hand, will be the newest ones included in the toolkit. | ||
This program is identical to the one above. However, other toolkit libraries will also be available to use, should you need them. | ||
|
||
### Using REPL | ||
### Using the REPL | ||
|
||
You can execute code interactively using REPL provided by the `scala` command. Execute `scala` in console without any arguments. | ||
You can execute code interactively using the REPL provided by the `scala` command. Execute `scala` in the console without any arguments. | ||
``` | ||
$ scala | ||
Welcome to Scala 3.5.0 (20-ea, Java OpenJDK 64-Bit Server VM). | ||
Welcome to Scala {{site.scala-3-version}} (20-ea, Java OpenJDK 64-Bit Server VM). | ||
Type in expressions for evaluation. Or try :help. | ||
|
||
scala> | ||
|
@@ -279,9 +285,6 @@ sbt and an IDE using the tutorials below. If you want to familiarize yourself wi | |
* [The Tour of Scala](/tour/tour-of-scala.html) for bite-sized introductions to Scala's features. | ||
* [Learning Resources](/learn.html), which includes online interactive tutorials and courses. | ||
* [Our list of some popular Scala books](/books.html). | ||
* [The migration guide](/scala3/guides/migration/compatibility-intro.html) helps you to migrate your existing Scala 2 code base to Scala 3. | ||
|
||
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. | ||
|
||
## Create a "Hello World" project with sbt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this section should be removed, or moved to a separate page. |
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Gedochao updating this variable seems to be missing from the release procedure