You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+37-29
Original file line number
Diff line number
Diff line change
@@ -24,14 +24,13 @@
24
24
| February 21, 2021 | Scala `3.0.0-RC1` updates |
25
25
| April 3, 2021 | Scala `3.0.0-RC2` updates |
26
26
| April 24, 2021 | Scala `3.0.0-RC3` updates |
27
+
| May 15, 2021 | Scala `3.0.0` final updates. Almost done! |
27
28
28
29
[](https://gitter.im/deanwampler/programming-scala-book-code-examples?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
29
30
30
-
This repo contains all the code examples to be included in [Programming Scala, Third Edition](http://programming-scala.org). (The second edition is [available here](http://shop.oreilly.com/product/0636920033073.do).) There are also many code files in this distribution that aren't included in the book.
31
+
This repo contains all the code examples in [Programming Scala, Third Edition](http://programming-scala.org). (The second edition is [available here](http://shop.oreilly.com/product/0636920033073.do).) There are also many code files in this distribution that aren't included in the book.
31
32
32
-
When the second edition was published, the examples used Scala 2.11. The code has since been updated to also compile with Scala 2.12 and 2.13. Many examples are being improved, in part so they compile with newer library versions and with the stricter compiler flags now used. (ScalaTest, in particular, has changed a lot since the second edition.)
33
-
34
-
If you want the example code for the second edition (with some bug fixes and Scala 2.12-2.13 support), [download the tagged 2.1.0](https://github.com/deanwampler/programming-scala-book-code-examples/releases/tag/2.1.0) build or check out the `release-2.1.0` branch. The latest `2.X.Y` release and `release-2.X.Y` branch include all the updates for 2.12 and 2.13. (No more `release-2.X.Y` releases are planned.) The third edition code for Scala 3 will be tagged with `release-3.0.0-XYZ`, with `XYZ` starting at `001` until the edition is published. Afterwards, `release-3.X.Y` will be used. These releases will be based off of `master`.
33
+
The `master` branch and the `3.X.Y` tag releases are for the third edition. The code examples for the second edition are still available. [Download the release tagged 2.1.0](https://github.com/deanwampler/programming-scala-book-code-examples/releases/tag/2.1.0) or check out the `release-2.1.0` branch. While the second edition was published for 2.11. The latest `2.1.0` release and `release-2.1.0` are updated for 2.12 and 2.13. (No more `release-2.X.Y` releases are planned.)
35
34
36
35
## How the Code Is Used in the Book
37
36
@@ -43,18 +42,18 @@ In the book's text, when an example corresponds to a file in this distribution,
43
42
44
43
Following the usual conventions, tests are in `src/test/...`.
45
44
46
-
Use these comments to find the corresponding source file. This archive also contains *MUnit* and *ScalaCheck* unit tests to validate some of the code. Most of these tests are not reproduced in the text of the book.
45
+
Use these comments to find the corresponding source file. This archive also contains *MUnit* and *ScalaCheck* unit tests to validate some of the code.
47
46
48
47
## Naming Conventions
49
48
50
-
The examples include "scripts" that are run with the `scala` command-line "REPL" (read, eval, print loop) or using `sbt console`. Other files are compiled.
49
+
The examples include "scripts" that are intended for interactive use in the `scala` command-line "REPL" (read, eval, print loop), for example using `sbt console` (where [`sbt`](http://www.scala-sbt.org/release/docs/Getting-Started/Setup.html) is the de facto build tool for Scala that I use). Other files are compiled.
51
50
52
-
To keep these different kinds of files straight and to support building with SBT, the following conventions are used for the files:
51
+
To keep these different kinds of files straight and to support building with `sbt`, the following conventions are used for the files:
53
52
54
-
-`src/main/scala/.../*.scala` - All Scala 3 source files built with SBT.
55
-
-`src/test/.../*.scala` - All Scala 3 test source files built and executed with SBT.
53
+
-`src/main/scala/.../*.scala` - All Scala 3 source files built with `sbt`.
54
+
-`src/test/.../*.scala` - All Scala 3 test source files built and executed with `sbt`.
56
55
-`src/script/.../*.scala` - "Script" files that won't compile with `scalac`, but can be interpreted with the `scala` REPL or used as a worksheet (see below).
57
-
-`src/*/scala-2/.../*.scala` - Some Scala 2 source files that won't compile with Scala 3. They are not built with SBT.
56
+
-`src/*/scala-2/.../*.scala` - Some Scala 2 source files that won't compile with Scala 3. They are not built with `sbt`.
58
57
59
58
## Other Notes about the Code
60
59
@@ -67,25 +66,25 @@ Some files have sections marked like this:
67
66
// end::section1[]
68
67
```
69
68
70
-
These are used to mark sections that are selectively included in the book. Sometimes the whole file is included in sections, while other times the file has content that isn't included ("Bonus!!").
69
+
These are used to mark sections that are selectively included in the book. Sometimes the whole file is included in sections, while other times the file has extra bonus content that doesn't appear in the book.
71
70
72
71
## Required and Optional Tools
73
72
74
-
To build and run the examples, all you need is a recent version of the the JDK and [SBT](http://www.scala-sbt.org/release/docs/Getting-Started/Setup.html), the de-facto standard build tool for Scala. When you run SBT, it will bootstrap itself with the correct version of its jar file, Scala, and project dependencies, which are specified in the `build.sbt` file in the root directory and other build files in the `project` directory.
73
+
To build and run the examples, all you need is a recent version of the the JDK and [`sbt`](http://www.scala-sbt.org/release/docs/Getting-Started/Setup.html). When you run `sbt`, it will bootstrap itself with the correct version of its jar file, Scala, and project dependencies, which are specified in the `build.sbt` file in the root directory and other build files in the `project` directory.
75
74
76
-
Follow these [installation instructions](http://www.scala-sbt.org/release/docs/Getting-Started/Setup.html).
75
+
Follow these [`sbt`installation instructions](http://www.scala-sbt.org/release/docs/Getting-Started/Setup.html).
77
76
78
-
If you want to install Scala separately and Scala's *Scaladocs*, go to [scala-lang.org](http://scala-lang.org) for details. However, this isn't required.
77
+
If you want to install Scala separately and Scala's *Scaladocs*, go to the [scala-lang.org_Getting Started_ guide](https://docs.scala-lang.org/scala3/getting-started.html) for details. However, this isn't required.
79
78
80
-
If you want to play with the Spark example, `src/script/scala-2/progscala3/bigdata/SparkWordCount.scala`, you'll need to download a Spark distribution from https://spark.apache.org and use a compatible version of Scala, 2.12 (at the time of this writing). With that version of Scala on your path and assuming that `$SPARK_HOME` refers to the root directory of your Spark installations, run the following command in the root directory of this project:
79
+
If you want to play with the Spark example, `src/script/scala-2/progscala3/bigdata/SparkWordCount.scala`, you'll need to download a Spark distribution from https://spark.apache.org. Assuming that `$SPARK_HOME` refers to the root directory of your Spark installation, run the following command in the root directory of this project:
81
80
82
81
```
83
82
$ $SPARK_HOME/bin/spark-shell
84
83
...
85
84
scala>
86
85
```
87
86
88
-
Then paste the content of `SparkWordCount.scala` at the prompt. After it runs, there will be a new directory, `README.md.wordcount` with the _partition_ files of the output.
87
+
Then copy and paste the content of `src/script/scala-2/progscala3/bigdata/SparkWordCount.scala` at the prompt. After it runs, there will be a new directory, `README.md.wordcount` with the _partition_ files of the output.
89
88
90
89
> **Tip:** For more on Spark, see my free tutorial on GitHub, [spark-scala-tutorial](https://github.com/deanwampler/spark-scala-tutorial).
91
90
@@ -95,29 +94,29 @@ Then paste the content of `SparkWordCount.scala` at the prompt. After it runs, t
95
94
96
95
Most editors and IDEs now have some sort of Scala support:
97
96
98
-
*[IntelliJ](https://www.jetbrains.com/idea/): Either the Community or Ultimate additions will work. Install the Scala plugin, which has built-in support for SBT.
97
+
*[IntelliJ](https://www.jetbrains.com/idea/): Either the Community or Ultimate additions will work. Install the Scala plugin, which has built-in support for `sbt`.
99
98
*[Visual Studio Code](https://code.visualstudio.com/): Use the new [Scala Metals](https://scalameta.org/metals/) plugin instead of older plugins.
100
99
*[Eclipse Scala IDE](http://scala-ide.org): Old, no longer recommended.
101
100
102
101
For other IDEs and text editors, try [Scala Metals](https://scalameta.org/metals/) first (I've used it with [Sublime Text](https://www.sublimetext.com/), for example) or the older [ENSIME](http://ensime.github.io/) project. You may also need additional, third-party tools for syntax highlighting, etc.
103
102
104
-
After installing the required plugins, load this project in your IDE, which should detect and use the SBT project automatically. For eclipse, run the `sbt eclipse` task to generate project files, then import them.
103
+
After installing the required plugins, load this project in your IDE, which should detect and use the `sbt` project automatically. For eclipse, run the `sbt eclipse` task to generate project files, then import them.
105
104
106
105
### Using Scala Worksheets
107
106
108
-
If you like working with _Scala worksheets_ in your IDE or editor, you may be able to load any of the REPL "script" files under `src/script` as a worksheet. If your environment is more restrictive, for example about the extension used, then run the included `bash` script `./make-worksheets.sh` to copy all the REPL "script" examples to worksheet files. This command copies the tree `src/script` to `src/worksheet` and changes the `.scala` extension for all the files to `.worksheet.sc`, the VSCode convention. These behaviors are configurable. Use the `--help` option to see the details. If you are using Windows and you don't have `bash` available, e.g., through the Linux subsystem, then modify individual files as you see fit.
107
+
If you like working with _Scala worksheets_ in your IDE or editor, you may be able to load any of the REPL "script" files under `src/script` as a worksheet. If your environment is more restrictive, for example about the file extension used, then run the included `bash` script `./make-worksheets.sh` to copy all the REPL "script" examples to worksheet files. This command copies the tree `src/script` to `src/worksheet` and changes the `.scala` extension for all the files to `.worksheet.sc`, the VSCode convention. These behaviors are configurable. Use the `--help` option to see the details. If you are using Windows and you don't have `bash` available, e.g., through the Linux subsystem, then modify individual files as you see fit.
109
108
110
109
See this [Dotty documentation page](https://dotty.epfl.ch/docs/usage/worksheet-mode.html) for more information about worksheets.
111
110
112
111
## Building the Code Examples
113
112
114
-
After installing SBT, open a command/terminal window and run the `sbt test` command.
113
+
After installing `sbt`, open a command/terminal window and run the `sbt test` command.
115
114
116
115
You'll see lots of output as it downloads all the dependencies, compiles the code and runs the tests. You should see `[success]` messages at the end.
117
116
118
-
SBT is discussed in more detail in the book and the [SBT website](https://www.scala-sbt.org/), but a few useful commands are worth mentioning here.
117
+
`sbt` is discussed in more detail in the book and the [`sbt` website](https://www.scala-sbt.org/), but a few useful commands are worth mentioning here.
119
118
120
-
If you start `sbt` without any arguments, it puts you into an interactive mode where you can type commands. Use control-D to exit this mode. Once at the SBT prompt (`sbt:programming-scala-3rd-ed-code-examples>`), try the following commands, where each `#` starts a comment; don't type those!
119
+
If you start `sbt` without any arguments, it puts you into an interactive mode where you can type commands. Use control-D to exit this mode. Once at the `sbt` prompt (`sbt:programming-scala-3rd-ed-code-examples>`), try the following commands, where each `#` starts a comment; don't type those!
121
120
122
121
```
123
122
help # help on tasks and settings
@@ -132,20 +131,29 @@ tasks -V # REALLY show ALL tasks
132
131
133
132
The `~` prefix causes the task to be run continuously each time source code changes are saved. This promotes continuous TDD (test-driven development) and is one of my favorite features!
134
133
135
-
Outside of SBT, you could, in principle, run the script files manually at the console/terminal prompt.
134
+
Outside of `sbt`, you could, in principle, run the REPL and load the script files manually at the prompt:
136
135
137
136
```
138
-
scala src/script/scala/.../Foo.scala
137
+
$ scala
138
+
scala> :load src/script/scala/.../Foo.scala
139
139
```
140
140
141
-
However, _at the time of this writing_, the `scala` REPL for the preview versions of Scala 3 do not support loading script files like this. Hopefully the final version of Scala 3 will restore this ability. Actually, it's easier to run many of the scripts using the SBT `console` task, at least those that use libraries or other compiled code that SBT has on the `CLASSPATH` already. Use the REPL's `:load src/script/scala/.../Foo.scala` feature to load and run a script file. You can also work with these files as Scala worksheets, as discussed above.
141
+
However, it's easier to run most of the scripts using `sbt console`, because `sbt` will configure the `CLASSPATH` with the third-party libraries and compiled code examples that a script file might use.
142
+
143
+
Also, new for the Scala 3 REPL, for those `src/main/...` files that define one (and only one) _entry point_, meaning a `main` method (Scala 2 compatible) or annotated with `@main` (new Scala 3 technique), you can compile and run them in one step:
I welcome feedback on the Book and these examples. Please post comments, corrections, etc. to one of the following places:
146
154
147
-
* This GitHub repo's [Gitter channel](https://gitter.im/deanwampler/programming-scala-book-code-examples), [Discussion forum](https://github.com/deanwampler/programming-scala-book-code-examples/discussions), or [Issues](https://github.com/deanwampler/programming-scala-book-code-examples/issues)
148
-
* The book's Twitter account, [@ProgScala](https://twitter.com/ProgScala)
149
-
* The O'Reilly book and errata sites, after publication.
155
+
* This GitHub repo's [Gitter channel](https://gitter.im/deanwampler/programming-scala-book-code-examples), [Discussion forum](https://github.com/deanwampler/programming-scala-book-code-examples/discussions), or [Issues](https://github.com/deanwampler/programming-scala-book-code-examples/issues).
156
+
* The book's Twitter account, [@ProgScala](https://twitter.com/ProgScala).
157
+
* The O'Reilly book and errata sites (coming soon).
150
158
151
-
There is also a dedicated site for the book where occasional updates, clarifications, corrections, and lame excuses will be posted: [programming-scala.org](http://programming-scala.org).
159
+
There is also my dedicated site for the book where occasional updates, clarifications, corrections, and lame excuses will be posted: [programming-scala.org](http://programming-scala.org).
0 commit comments