-
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
Changes from 3 commits
23d6dda
0ff5ba0
0959275
a85e51e
8e84db6
57efc0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -145,6 +145,144 @@ To install them manually: | |||||||||||||
or [AdoptOpenJDK 8/11](https://adoptopenjdk.net/). Refer to [JDK Compatibility](/overviews/jdk-compatibility/overview.html) for Scala/Java compatibility detail. | ||||||||||||||
1. Install [sbt](https://www.scala-sbt.org/download.html) | ||||||||||||||
|
||||||||||||||
## Using the Scala CLI | ||||||||||||||
|
||||||||||||||
Create a file named `hello.scala` with the following code: | ||||||||||||||
```scala | ||||||||||||||
@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!") | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
You can define a method with the `def` keyword and mark it as a "main" method with the `@main` annotation, designating it as | ||||||||||||||
the entry point in program execution. The method's type is `Unit`, which means it does not return a value. `Unit` | ||||||||||||||
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. 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 |
Outdated
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.
there's also a liquid variable {{site.scala-3-version}}
which can automatically update when we have a new release. e.g. its used on this page in the dropdown "Testing your setup"
Outdated
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.
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. |
Same thing everywhere. The existing style of this page is to use imperative tenses for all the things the user should do. We should stick to that writing style.
Outdated
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.
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](<link to toolkit page>) for that purpose. A dependency on the library can | |
be added with the `//> using` directive. Put the following code in `counter.scala`. |
I'm all for giving credit where credit is due, but in this "getting started" page shown to brand new users, they will wonder why we use this "obscure library written by some random person". Saying "from the Scala toolkit" gives it a more official standing here.
(also: simpler sentences)
Outdated
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.
def countFiles(): Unit = | |
val paths = os.list(os.pwd) | |
println(paths.length) | |
def countFiles(): Unit = | |
val paths = os.list(os.pwd) | |
println(paths.length) |
Outdated
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.
Simpler sentences, active voice:
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 | |
sequence of paths. | |
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. |
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.
TBH, I'm not sure showing the alternative with the toolkit is a good idea here. We should definitely link to the toolkit's home page as a "next steps" scenario. But with what we already showed above, the user already "got started". There is no need to switch to the toolkit for no immediate reason.
gkepka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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.
Use a real version number, for reproducible outputs. We definitely don't want this page's example to stop working when a new version of the toolkit gets released.
Outdated
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.
def countFiles(): Unit = | |
val paths = os.list(os.pwd) | |
println(paths.length) | |
def countFiles(): Unit = | |
val paths = os.list(os.pwd) | |
println(paths.length) |
Outdated
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.
Shorter sentences:
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. |
Outdated
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.
### Using REPL | |
### Using the REPL |
Outdated
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.
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. |
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.
Looks like this section should be removed, or moved to a separate page.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,53 +47,27 @@ object hello { | |
{% endtabs %} | ||
<!-- End tabs --> | ||
|
||
Next, compile the code with `scalac`: | ||
Next, compile and run the code with `scala`: | ||
|
||
```bash | ||
$ scalac hello.scala | ||
$ scala run hello.scala | ||
``` | ||
|
||
If you’re coming to Scala from Java, `scalac` is just like `javac`, so that command creates several files: | ||
When you run the command for the first time, two hidden directories will be created: `.bsp` and `.scala-build`. The first | ||
one contains the [Build Server Protocol](https://build-server-protocol.github.io/) information for IDEs, and the second one contains the results | ||
of compilation. | ||
|
||
|
||
<!-- Display Hello World compiled outputs for each Scala Version --> | ||
{% tabs hello-world-outputs class=tabs-scala-version %} | ||
|
||
{% tab 'Scala 2' for=hello-world-outputs %} | ||
```bash | ||
$ ls -1 | ||
hello$.class | ||
hello.class | ||
hello.scala | ||
The command should produce similar output: | ||
``` | ||
{% endtab %} | ||
|
||
{% tab 'Scala 3' for=hello-world-outputs %} | ||
```bash | ||
$ ls -1 | ||
hello$package$.class | ||
hello$package.class | ||
hello$package.tasty | ||
hello.scala | ||
hello.class | ||
hello.tasty | ||
``` | ||
{% endtab %} | ||
|
||
{% endtabs %} | ||
<!-- End tabs --> | ||
|
||
Like Java, the _.class_ files are bytecode files, and they’re ready to run in the JVM. | ||
|
||
Now you can run the `hello` method with the `scala` command: | ||
|
||
```bash | ||
$ scala hello | ||
Compiling project (Scala 3.5.0, JVM (20)) | ||
Compiled project (Scala 3.5.0, JVM (20)) | ||
Hello, World! | ||
``` | ||
|
||
Assuming that worked, congratulations, you just compiled and ran your first Scala application. | ||
|
||
> More information about sbt and other tools that make Scala development easier can be found in the [Scala Tools][scala_tools] chapter. | ||
> The Scala CLI documentation can be found [here](https://scala-cli.virtuslab.org/). | ||
|
||
## Ask For User Input | ||
|
||
|
@@ -152,24 +126,23 @@ use the `+` operator on strings to join `"Hello, "` with `name` and `"!"`, makin | |
|
||
> You can learn more about using `val` by reading [Variables and Data Types](/scala3/book/taste-vars-data-types.html). | ||
|
||
Then compile the code with `scalac`: | ||
|
||
```bash | ||
$ scalac helloInteractive.scala | ||
``` | ||
Then run it with `scala helloInteractive`, this time the program will pause after asking for your name, | ||
Then run the code with `scala`. This time the program will pause after asking for your name, | ||
and wait until you type a name and press return on the keyboard, looking like this: | ||
|
||
```bash | ||
$ scala helloInteractive | ||
$ scala run helloInteractive.scala | ||
Compiling project (Scala 3.5.0, JVM (20)) | ||
Compiled project (Scala 3.5.0, JVM (20)) | ||
Please enter your name: | ||
▌ | ||
``` | ||
|
||
When you enter your name at the prompt, the final interaction should look like this: | ||
|
||
```bash | ||
$ scala helloInteractive | ||
$ scala run helloInteractive.scala | ||
Compiling project (Scala 3.5.0, JVM (20)) | ||
Compiled project (Scala 3.5.0, JVM (20)) | ||
Please enter your name: | ||
Alvin Alexander | ||
Hello, Alvin Alexander! | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -160,38 +160,49 @@ package, so can be accessed from anywhere in a program. | |||||
|
||||||
> **Note:** The following assumes you are using Scala on the command line | ||||||
|
||||||
If we save the above program in a file called | ||||||
`HelloWorld.scala`, we can run it by issuing the following | ||||||
command (the greater-than sign `>` represents the shell prompt | ||||||
and should not be typed): | ||||||
|
||||||
```shell | ||||||
> scala run HelloWorld.scala | ||||||
``` | ||||||
|
||||||
The program will be automatically compiled (with compiled classes somewhere in the newly created `.scala-build` directory) | ||||||
and executed, producing a similar output: | ||||||
|
and executed, producing a similar output: | |
and executed, producing an output similar to: |
("a similar output" in English refers to another output that was previously mentioned; there is no such thing here)
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.