From 23d6dda0ea91c92e705e385eed176a84af68e3b2 Mon Sep 17 00:00:00 2001 From: gkepka Date: Mon, 16 Sep 2024 10:04:10 +0200 Subject: [PATCH 1/5] Use scala-cli --- _overviews/getting-started/index.md | 148 ++++++++++++++++-- _overviews/scala-book/two-types-variables.md | 3 +- .../scala3-book/methods-main-methods.md | 18 +-- _overviews/scala3-book/taste-hello-world.md | 59 ++----- .../tutorials/scala-for-java-programmers.md | 37 +++-- 5 files changed, 187 insertions(+), 78 deletions(-) diff --git a/_overviews/getting-started/index.md b/_overviews/getting-started/index.md index 8c33b915dd..6ffc8444b7 100644 --- a/_overviews/getting-started/index.md +++ b/_overviews/getting-started/index.md @@ -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 command + +Create a file named `hello.scala` with following code: +```scala +@main +def hello(): Unit = + println("Hello, World!") +``` + +You can define a method with `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 `void` keyword found in other languages. The `println` method will print the `"Hello World!"` +string to standard output. + +To run the program, execute `scala-cli run hello.scala` command. The file will be compiled and executed, with console output +similar to following: +``` +$ scala-cli run hello.scala +Compiling project (Scala 3.4.2, JVM (20)) +Compiled project (Scala 3.4.2, JVM (20)) +Hello, World! +``` + +### Handling command-line arguments + +Let's rewrite the `hello.scala` file so that the program greets the person running it. +```scala +@main +def hello(name: String): Unit = + println(s"Hello, $name!") +``` + +The `name` argument is expected to be provided when executing the program and if it's not found, the execution will fail. +The `println` method receives an interpolated string, as indicated by the `s` letter preceding its content. `$name` will be substituted by +the content of `name` argument. + +To pass the arguments when executing the program, put them after `--`: +``` +$ scala-cli run hello.scala -- Gabriel +Compiling project (Scala 3.4.2, JVM (20)) +Compiled project (Scala 3.4.2, 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 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 `//> using` directive. Put the following code in `counter.scala`. +```scala +//> using dep "com.lihaoyi::os-lib:0.10.7" + +@main +def countFiles(): Unit = + val paths = os.list(os.pwd) + println(paths.length) +``` + +In the code above, the `os.pwd` returns current working directory, which then is passed to `os.list`, which returns a sequence +of paths directly within the directory passed as argument. `val` is used 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-cli run counter.scala +Compiling project (Scala 3.4.2, JVM (20)) +Compiled project (Scala 3.4.2, JVM (20)) +4 +``` +The printed number should be 4: `hello.scala`, `counter.scala` and two hidden directories created automatically when a program is executed: +`.bsp` containing information about project used by IDEs, and `.scala-build` containing the results of compilation. + +As it turns out, the `os-lib` library is part of Scala Toolkit, a collection of libraries recommended for tasks like testing, +operating system interaction or handling JSONs. You can read more about libraries included in the toolkit [here](/toolkit/introduction.html). +To include the toolkit libraries, use `//> using toolkit default` directive: +```scala +//> using toolkit default + +@main +def countFiles(): Unit = + 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 toolkit. + +### Using scala-cli REPL + +You can execute code interactively using REPL provided by `scala-cli` command. Execute `scala-cli` in console without any arguments. +``` +$ scala-cli +Welcome to Scala 3.4.2 (20-ea, Java OpenJDK 64-Bit Server VM). +Type in expressions for evaluation. Or try :help. + +scala> +``` + +Write a line of code to be executed and press enter. +``` +scala> println("Hello, World!") +Hello, World! + +scala> +``` + +The result will be printed immediately after executing the line. You can declare values: +``` +scala> val i = 1 +val i: Int = 1 + +scala> +``` + +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. +``` +scala> i + 3 +val res0: Int = 4 + +scala> +``` +You can exit the REPL with `:exit`. + +### Next steps + +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 +sbt and an IDE using the tutorials below. If you want to familiarize yourself with the language more, consider checking out: + +* [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. +* [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 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: When you’re finished experimenting with this project, press `[Enter]` to interrupt the `run` command. Then type `exit` or press `[Ctrl+D]` to exit sbt and return to your command line prompt. -## Next Steps - -Once you've finished the above tutorials, consider checking out: - -* [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. -* [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. - ## Getting Help 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 --git a/_overviews/scala-book/two-types-variables.md b/_overviews/scala-book/two-types-variables.md index 8c6a105c79..8aa67a439a 100644 --- a/_overviews/scala-book/two-types-variables.md +++ b/_overviews/scala-book/two-types-variables.md @@ -94,8 +94,7 @@ object Hello3 extends App { As before: - Save that code in a file named *Hello3.scala* -- Compile it with `scalac Hello3.scala` -- Run it with `scala Hello3` +- Compile and run it with `scala-cli run Hello3.scala` diff --git a/_overviews/scala3-book/methods-main-methods.md b/_overviews/scala3-book/methods-main-methods.md index 797d7d04a4..28db85ca6f 100644 --- a/_overviews/scala3-book/methods-main-methods.md +++ b/_overviews/scala3-book/methods-main-methods.md @@ -22,10 +22,10 @@ Scala 3 offers a new way to define programs that can be invoked from the command {% endtab %} {% endtabs %} -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`: +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`: ```bash -$ scala Hello.scala +$ scala-cli run Hello.scala Hello, World ``` @@ -64,10 +64,10 @@ For example, given this `@main` method that takes an `Int`, a `String`, and a va {% endtab %} {% endtabs %} -When you compile that code, it creates a main program named `happyBirthday` that’s called like this: +Pass the arguments after `--`: ``` -$ scala happyBirthday 23 Lisa Peter +$ scala-cli run happyBirthday.scala -- 23 Lisa Peter Happy 23rd Birthday, Lisa and Peter! ``` @@ -79,10 +79,10 @@ The program implemented from an `@main` method checks that there are enough argu If a check fails, the program is terminated with an error message: ``` -$ scala happyBirthday 22 +$ scala-cli run happyBirthday.scala -- 22 Illegal command line after first argument: more arguments expected -$ scala happyBirthday sixty Fred +$ scala-cli run happyBirthday.scala -- sixty Fred Illegal command line: java.lang.NumberFormatException: For input string: "sixty" ``` @@ -176,11 +176,9 @@ object happyBirthday { {% endtab %} {% endtabs %} -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: +If you place that code in a file named *happyBirthday.scala*, you can then compile and run it with `scala-cli`, as shown previously: ```bash -$ scalac happyBirthday.scala - -$ scala happyBirthday 23 Lisa Peter +$ scala-cli run happyBirthday.scala -- 23 Lisa Peter Happy 23rd Birthday, Lisa and Peter! ``` diff --git a/_overviews/scala3-book/taste-hello-world.md b/_overviews/scala3-book/taste-hello-world.md index 4bdf996f08..df6771e023 100644 --- a/_overviews/scala3-book/taste-hello-world.md +++ b/_overviews/scala3-book/taste-hello-world.md @@ -47,53 +47,27 @@ object hello { {% endtabs %} -Next, compile the code with `scalac`: +Next, compile and run the code with `scala-cli`: ```bash -$ scalac hello.scala +$ scala-cli 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, the second one contains the results +of compilation. - -{% 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 %} - - -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.4.2, JVM (20)) +Compiled project (Scala 3.4.2, 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,16 +126,13 @@ 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-cli`. 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-cli run helloInteractive.scala +Compiling project (Scala 3.4.2, JVM (20)) +Compiled project (Scala 3.4.2, JVM (20)) Please enter your name: ▌ ``` @@ -169,7 +140,9 @@ Please enter your name: When you enter your name at the prompt, the final interaction should look like this: ```bash -$ scala helloInteractive +$ scala-cli run helloInteractive.scala +Compiling project (Scala 3.4.2, JVM (20)) +Compiled project (Scala 3.4.2, JVM (20)) Please enter your name: Alvin Alexander Hello, Alvin Alexander! diff --git a/_overviews/tutorials/scala-for-java-programmers.md b/_overviews/tutorials/scala-for-java-programmers.md index bc4a551d91..98cea7e7cb 100644 --- a/_overviews/tutorials/scala-for-java-programmers.md +++ b/_overviews/tutorials/scala-for-java-programmers.md @@ -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-cli 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: +``` +Compiling project (Scala 3.4.2, JVM (20)) +Compiled project (Scala 3.4.2, JVM (20)) +Hello, World! +``` + #### Compiling From the Command Line -To compile the example, we use `scalac`, the Scala compiler. `scalac` +To compile the example, we use `scala-cli compile` command, which will invoke the Scala compiler, `scalac`. `scalac` works like most compilers: it takes a source file as argument, maybe some options, and produces one or several output files. The outputs it produces are standard Java class files. -If we save the above program in a file called -`HelloWorld.scala`, we can compile it by issuing the following -command (the greater-than sign `>` represents the shell prompt -and should not be typed): - ```shell -> scalac HelloWorld.scala +> scala-cli compile HelloWorld.scala -d . ``` -This will generate a few class files in the current directory. One of +This will generate a few class files in the current directory (`-d .` option sets the compilation output directory). One of them will be called `HelloWorld.class`, and contains a class -which can be directly executed using the `scala` command, as the +which can be directly executed using the `scala-cli` command, as the following section shows. #### Running From the Command Line -Once compiled, a Scala program can be run using the `scala` command. +Once compiled, the program can be run using the `scala-cli run` command. Its usage is very similar to the `java` command used to run Java -programs, and accepts the same options. The above example can be +programs, and accepts similar options. The above example can be executed using the following command, which produces the expected output: ```shell -> scala -classpath . HelloWorld - +> scala-cli run --main-class HelloWorld -classpath . Hello, World! ``` From 0ff5ba0f1ec0f34972283ca1a28bbf9eabbec950 Mon Sep 17 00:00:00 2001 From: gkepka Date: Tue, 17 Sep 2024 09:59:22 +0200 Subject: [PATCH 2/5] Change scala-cli to scala --- _overviews/getting-started/index.md | 34 +++++++++---------- _overviews/scala-book/two-types-variables.md | 2 +- .../scala3-book/methods-main-methods.md | 14 ++++---- _overviews/scala3-book/taste-hello-world.md | 22 ++++++------ .../tutorials/scala-for-java-programmers.md | 18 +++++----- 5 files changed, 45 insertions(+), 45 deletions(-) diff --git a/_overviews/getting-started/index.md b/_overviews/getting-started/index.md index 6ffc8444b7..8d1a06b37e 100644 --- a/_overviews/getting-started/index.md +++ b/_overviews/getting-started/index.md @@ -145,7 +145,7 @@ 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 command +## Using the Scala CLI Create a file named `hello.scala` with following code: ```scala @@ -156,15 +156,15 @@ def hello(): Unit = You can define a method with `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 `void` keyword found in other languages. The `println` method will print the `"Hello World!"` +can be thought of as an analogue to `void` keyword found in other languages. The `println` method will print the `"Hello, World!"` string to standard output. -To run the program, execute `scala-cli 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 similar to following: ``` -$ scala-cli run hello.scala -Compiling project (Scala 3.4.2, JVM (20)) -Compiled project (Scala 3.4.2, JVM (20)) +$ scala run hello.scala +Compiling project (Scala 3.5.0, JVM (20)) +Compiled project (Scala 3.5.0, JVM (20)) Hello, World! ``` @@ -183,9 +183,9 @@ the content of `name` argument. To pass the arguments when executing the program, put them after `--`: ``` -$ scala-cli run hello.scala -- Gabriel -Compiling project (Scala 3.4.2, JVM (20)) -Compiled project (Scala 3.4.2, JVM (20)) +$ scala run hello.scala -- Gabriel +Compiling project (Scala 3.5.0, JVM (20)) +Compiled project (Scala 3.5.0, JVM (20)) Hello, Gabriel! ``` @@ -211,15 +211,15 @@ sequence of paths. Execute the program. The dependency will be automatically downloaded. The execution should result in a similar output: ``` -$ scala-cli run counter.scala -Compiling project (Scala 3.4.2, JVM (20)) -Compiled project (Scala 3.4.2, JVM (20)) +$ scala run counter.scala +Compiling project (Scala 3.5.0, JVM (20)) +Compiled project (Scala 3.5.0, JVM (20)) 4 ``` The printed number should be 4: `hello.scala`, `counter.scala` and two hidden directories created automatically when a program is executed: `.bsp` containing information about project used by IDEs, and `.scala-build` containing the results of compilation. -As it turns out, the `os-lib` library is part of Scala Toolkit, a collection of libraries recommended for tasks like testing, +As it turns out, the `os-lib` library is a part of Scala Toolkit, a collection of libraries recommended for tasks like testing, operating system interaction or handling JSONs. You can read more about libraries included in the toolkit [here](/toolkit/introduction.html). To include the toolkit libraries, use `//> using toolkit default` directive: ```scala @@ -234,12 +234,12 @@ def countFiles(): Unit = 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 toolkit. -### Using scala-cli REPL +### Using REPL -You can execute code interactively using REPL provided by `scala-cli` command. Execute `scala-cli` in console without any arguments. +You can execute code interactively using REPL provided by `scala` command. Execute `scala` in console without any arguments. ``` -$ scala-cli -Welcome to Scala 3.4.2 (20-ea, Java OpenJDK 64-Bit Server VM). +$ scala +Welcome to Scala 3.5.0 (20-ea, Java OpenJDK 64-Bit Server VM). Type in expressions for evaluation. Or try :help. scala> diff --git a/_overviews/scala-book/two-types-variables.md b/_overviews/scala-book/two-types-variables.md index 8aa67a439a..3ce00a0e54 100644 --- a/_overviews/scala-book/two-types-variables.md +++ b/_overviews/scala-book/two-types-variables.md @@ -94,7 +94,7 @@ object Hello3 extends App { As before: - Save that code in a file named *Hello3.scala* -- Compile and run it with `scala-cli run Hello3.scala` +- Compile and run it with `scala run Hello3.scala` diff --git a/_overviews/scala3-book/methods-main-methods.md b/_overviews/scala3-book/methods-main-methods.md index 28db85ca6f..e2a30abfb1 100644 --- a/_overviews/scala3-book/methods-main-methods.md +++ b/_overviews/scala3-book/methods-main-methods.md @@ -22,10 +22,10 @@ Scala 3 offers a new way to define programs that can be invoked from the command {% endtab %} {% endtabs %} -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`: +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`: ```bash -$ scala-cli run Hello.scala +$ scala run Hello.scala Hello, World ``` @@ -67,7 +67,7 @@ For example, given this `@main` method that takes an `Int`, a `String`, and a va Pass the arguments after `--`: ``` -$ scala-cli run happyBirthday.scala -- 23 Lisa Peter +$ scala run happyBirthday.scala -- 23 Lisa Peter Happy 23rd Birthday, Lisa and Peter! ``` @@ -79,10 +79,10 @@ The program implemented from an `@main` method checks that there are enough argu If a check fails, the program is terminated with an error message: ``` -$ scala-cli run happyBirthday.scala -- 22 +$ scala run happyBirthday.scala -- 22 Illegal command line after first argument: more arguments expected -$ scala-cli run happyBirthday.scala -- sixty Fred +$ scala run happyBirthday.scala -- sixty Fred Illegal command line: java.lang.NumberFormatException: For input string: "sixty" ``` @@ -176,9 +176,9 @@ object happyBirthday { {% endtab %} {% endtabs %} -If you place that code in a file named *happyBirthday.scala*, you can then compile and run it with `scala-cli`, as shown previously: +If you place that code in a file named *happyBirthday.scala*, you can then compile and run it with `scala`, as shown previously: ```bash -$ scala-cli run happyBirthday.scala -- 23 Lisa Peter +$ scala run happyBirthday.scala -- 23 Lisa Peter Happy 23rd Birthday, Lisa and Peter! ``` diff --git a/_overviews/scala3-book/taste-hello-world.md b/_overviews/scala3-book/taste-hello-world.md index df6771e023..adfdc7c25d 100644 --- a/_overviews/scala3-book/taste-hello-world.md +++ b/_overviews/scala3-book/taste-hello-world.md @@ -47,10 +47,10 @@ object hello { {% endtabs %} -Next, compile and run the code with `scala-cli`: +Next, compile and run the code with `scala`: ```bash -$ scala-cli run hello.scala +$ scala run hello.scala ``` When you run the command for the first time, two hidden directories will be created: `.bsp` and `.scala-build`. The first @@ -59,8 +59,8 @@ of compilation. The command should produce similar output: ``` -Compiling project (Scala 3.4.2, JVM (20)) -Compiled project (Scala 3.4.2, JVM (20)) +Compiling project (Scala 3.5.0, JVM (20)) +Compiled project (Scala 3.5.0, JVM (20)) Hello, World! ``` @@ -126,13 +126,13 @@ 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 run the code with `scala-cli`. 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-cli run helloInteractive.scala -Compiling project (Scala 3.4.2, JVM (20)) -Compiled project (Scala 3.4.2, JVM (20)) +$ scala run helloInteractive.scala +Compiling project (Scala 3.5.0, JVM (20)) +Compiled project (Scala 3.5.0, JVM (20)) Please enter your name: ▌ ``` @@ -140,9 +140,9 @@ Please enter your name: When you enter your name at the prompt, the final interaction should look like this: ```bash -$ scala-cli run helloInteractive.scala -Compiling project (Scala 3.4.2, JVM (20)) -Compiled project (Scala 3.4.2, JVM (20)) +$ 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! diff --git a/_overviews/tutorials/scala-for-java-programmers.md b/_overviews/tutorials/scala-for-java-programmers.md index 98cea7e7cb..2bfda72ec6 100644 --- a/_overviews/tutorials/scala-for-java-programmers.md +++ b/_overviews/tutorials/scala-for-java-programmers.md @@ -166,43 +166,43 @@ command (the greater-than sign `>` represents the shell prompt and should not be typed): ```shell -> scala-cli run HelloWorld.scala +> 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: ``` -Compiling project (Scala 3.4.2, JVM (20)) -Compiled project (Scala 3.4.2, JVM (20)) +Compiling project (Scala 3.5.0, JVM (20)) +Compiled project (Scala 3.5.0, JVM (20)) Hello, World! ``` #### Compiling From the Command Line -To compile the example, we use `scala-cli compile` command, which will invoke the Scala compiler, `scalac`. `scalac` +To compile the example, we use `scala compile` command, which will invoke the Scala compiler, `scalac`. `scalac` works like most compilers: it takes a source file as argument, maybe some options, and produces one or several output files. The outputs it produces are standard Java class files. ```shell -> scala-cli compile HelloWorld.scala -d . +> scala compile HelloWorld.scala -d . ``` -This will generate a few class files in the current directory (`-d .` option sets the compilation output directory). One of +This will generate a few class files in the current directory (`-d` option sets the compilation output directory). One of them will be called `HelloWorld.class`, and contains a class -which can be directly executed using the `scala-cli` command, as the +which can be directly executed using the `scala` command, as the following section shows. #### Running From the Command Line -Once compiled, the program can be run using the `scala-cli run` command. +Once compiled, the program can be run using the `scala run` command. Its usage is very similar to the `java` command used to run Java programs, and accepts similar options. The above example can be executed using the following command, which produces the expected output: ```shell -> scala-cli run --main-class HelloWorld -classpath . +> scala run --main-class HelloWorld -classpath . Hello, World! ``` From 09592752bebd599594fd50b9671ca6bcd85bc3d2 Mon Sep 17 00:00:00 2001 From: gkepka Date: Tue, 17 Sep 2024 10:34:31 +0200 Subject: [PATCH 3/5] Link and grammar fixes --- _overviews/getting-started/index.md | 30 ++++++++++----------- _overviews/scala3-book/taste-hello-world.md | 2 +- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/_overviews/getting-started/index.md b/_overviews/getting-started/index.md index 8d1a06b37e..19052399e3 100644 --- a/_overviews/getting-started/index.md +++ b/_overviews/getting-started/index.md @@ -147,16 +147,16 @@ To install them manually: ## Using the Scala CLI -Create a file named `hello.scala` with following code: +Create a file named `hello.scala` with the following code: ```scala @main def hello(): Unit = println("Hello, World!") ``` -You can define a method with `def` keyword and mark it as a "main" method with the `@main` annotation, designating it as +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 `void` keyword found in other languages. The `println` method will print the `"Hello, World!"` +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 @@ -177,9 +177,9 @@ def hello(name: String): Unit = println(s"Hello, $name!") ``` -The `name` argument is expected to be provided when executing the program and if it's not found, the execution will fail. +The `name` argument is expected to be provided when executing the program, and if it's not found, the execution will fail. The `println` method receives an interpolated string, as indicated by the `s` letter preceding its content. `$name` will be substituted by -the content of `name` argument. +the content of the `name` argument. To pass the arguments when executing the program, put them after `--`: ``` @@ -189,13 +189,13 @@ Compiled project (Scala 3.5.0, 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. +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 Java API for +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 `//> using` directive. Put the following code in `counter.scala`. +be added with the `//> using` directive. Put the following code in `counter.scala`. ```scala //> using dep "com.lihaoyi::os-lib:0.10.7" @@ -205,8 +205,8 @@ def countFiles(): Unit = println(paths.length) ``` -In the code above, the `os.pwd` returns current working directory, which then is passed to `os.list`, which returns a sequence -of paths directly within the directory passed as argument. `val` is used to declare an immutable value, in this example storing the +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. Execute the program. The dependency will be automatically downloaded. The execution should result in a similar output: @@ -220,8 +220,8 @@ The printed number should be 4: `hello.scala`, `counter.scala` and two hidden di `.bsp` containing information about project used by IDEs, and `.scala-build` containing the results of compilation. As it turns out, the `os-lib` library is a part of Scala Toolkit, a collection of libraries recommended for tasks like testing, -operating system interaction or handling JSONs. You can read more about libraries included in the toolkit [here](/toolkit/introduction.html). -To include the toolkit libraries, use `//> using toolkit default` directive: +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: ```scala //> using toolkit default @@ -231,12 +231,12 @@ def countFiles(): Unit = 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 toolkit. +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. ### Using REPL -You can execute code interactively using REPL provided by `scala` command. Execute `scala` in console without any arguments. +You can execute code interactively using REPL provided by the `scala` command. Execute `scala` in console without any arguments. ``` $ scala Welcome to Scala 3.5.0 (20-ea, Java OpenJDK 64-Bit Server VM). diff --git a/_overviews/scala3-book/taste-hello-world.md b/_overviews/scala3-book/taste-hello-world.md index adfdc7c25d..ff585d9463 100644 --- a/_overviews/scala3-book/taste-hello-world.md +++ b/_overviews/scala3-book/taste-hello-world.md @@ -54,7 +54,7 @@ $ scala run hello.scala ``` 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, the second one contains the results +one contains the [Build Server Protocol](https://build-server-protocol.github.io/) information for IDEs, and the second one contains the results of compilation. The command should produce similar output: From a85e51e16b8075b969532323e352c2f54fe3b71c Mon Sep 17 00:00:00 2001 From: gkepka Date: Tue, 8 Oct 2024 12:19:40 +0200 Subject: [PATCH 4/5] Grammar fixes + Scala 3 version update --- _config.yml | 2 +- _overviews/getting-started/index.md | 59 ++++++++++--------- _overviews/scala3-book/taste-hello-world.md | 18 +++--- .../tutorials/scala-for-java-programmers.md | 6 +- _ru/getting-started/index.md | 3 +- _uk/getting-started/index.md | 3 +- api/all.md | 4 +- 7 files changed, 48 insertions(+), 47 deletions(-) diff --git a/_config.yml b/_config.yml index f257fada05..170a1f41a0 100644 --- a/_config.yml +++ b/_config.yml @@ -17,7 +17,7 @@ keywords: scala-version: 2.13.14 scala-212-version: 2.12.19 -scala-3-version: 3.4.2 +scala-3-version: 3.5.1 collections: style: diff --git a/_overviews/getting-started/index.md b/_overviews/getting-started/index.md index 19052399e3..11bc59e720 100644 --- a/_overviews/getting-started/index.md +++ b/_overviews/getting-started/index.md @@ -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 %} @@ -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 ``, create a file named `hello.scala` with the following code: ```scala +//> using scala {{site.scala-3-version}} + @main 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 `` 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,8 +189,8 @@ 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! ``` @@ -193,27 +198,28 @@ You can read more about [main methods](/scala3/book/methods-main-methods.html) a ### 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: ```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 diff --git a/_overviews/scala3-book/taste-hello-world.md b/_overviews/scala3-book/taste-hello-world.md index ff585d9463..52fc532e5e 100644 --- a/_overviews/scala3-book/taste-hello-world.md +++ b/_overviews/scala3-book/taste-hello-world.md @@ -53,14 +53,10 @@ Next, compile and run the code with `scala`: $ scala run hello.scala ``` -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. - -The command should produce similar output: +The command should produce an output similar to: ``` -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! ``` @@ -131,8 +127,8 @@ and wait until you type a name and press return on the keyboard, looking like th ```bash $ scala run helloInteractive.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)) Please enter your name: ▌ ``` @@ -141,8 +137,8 @@ When you enter your name at the prompt, the final interaction should look like t ```bash $ scala run helloInteractive.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)) Please enter your name: Alvin Alexander Hello, Alvin Alexander! diff --git a/_overviews/tutorials/scala-for-java-programmers.md b/_overviews/tutorials/scala-for-java-programmers.md index 2bfda72ec6..67e00799bd 100644 --- a/_overviews/tutorials/scala-for-java-programmers.md +++ b/_overviews/tutorials/scala-for-java-programmers.md @@ -170,10 +170,10 @@ and should not be typed): ``` 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 an output similar to: ``` -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! ``` diff --git a/_ru/getting-started/index.md b/_ru/getting-started/index.md index e9d8e0beeb..f6968753f5 100644 --- a/_ru/getting-started/index.md +++ b/_ru/getting-started/index.md @@ -90,7 +90,8 @@ newcomer_resources: Проверьте корректность установки с помощью команды `scala -version`, которая должна вывести: ```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}} ``` Если сообщение не выдано, возможно, необходимо перезайти в терминал (или перезагрузиться), чтобы изменения вступили в силу. diff --git a/_uk/getting-started/index.md b/_uk/getting-started/index.md index e8275ada94..9725b362e9 100644 --- a/_uk/getting-started/index.md +++ b/_uk/getting-started/index.md @@ -75,7 +75,8 @@ _Scastie_ це онлайн “пісочниця”, де ви можете е Перевірте ваші налаштування виконавши команду `scala -version`, яка має вивести: ```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 %} diff --git a/api/all.md b/api/all.md index 334ecac392..2ca319f7c2 100644 --- a/api/all.md +++ b/api/all.md @@ -8,8 +8,8 @@ redirect_from: ## Latest releases -* Scala 3.4.2 - * [Library API](https://www.scala-lang.org/api/3.4.2/) +* Scala {{site.scala-3-version}} + * [Library API](https://www.scala-lang.org/api/{{site.scala-3-version}}/) * Scala 3.3.3 LTS * [Library API](https://www.scala-lang.org/api/3.3.3/) * Scala 2.13.14 From 8e84db6cdc0b7137dd7092396176a2ff7bd5bcad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20K=C4=99pka?= <44374670+gkepka@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:12:57 +0200 Subject: [PATCH 5/5] Use toolkit 0.5.0 version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Doeraene --- _overviews/getting-started/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/getting-started/index.md b/_overviews/getting-started/index.md index 11bc59e720..985b5222d4 100644 --- a/_overviews/getting-started/index.md +++ b/_overviews/getting-started/index.md @@ -227,7 +227,7 @@ The printed number should be 4: `hello.scala`, `counter.scala` and two hidden di As it turns out, the `os-lib` library is a part of Scala Toolkit, a collection of libraries recommended for tasks like testing, 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: +To include the toolkit libraries, use the `//> using toolkit 0.5.0` directive: ```scala //> using scala {{site.scala-3-version}} //> using toolkit 0.5.0