All the code in this book has been tested with Scala Native 0.4.0-M2, Scala 2.11.12, and sbt 0.13.15 on Mac OS X and Linux (via Docker). You can get detailed instructions on how to set up a Scala Native development environment for Mac, Windows, or Linux in Appendix 1, Setting Up the Environment. Since slight environment differences can be disruptive to low-level programs, I recommend using Docker on your preferred development machine, and the examples in the text reflect that.
A Note on Versions | |
---|---|
Scala Native is rapidly evolving. The example code in this chapter, as well as all other code in this book, is written for the most recent version of Scala Native available, 0.4.0-M2. To ensure forward compatibility, all of the sbt projects include a compatibility shim file; to use with a newer version, just remove the shim! You may also notice all the code is for Scala 2.11. When Scala 2.12 and 2.13 become available for Scala Native, I’ll update the code files as well. You can download the latest version of the sample code on the pragprog.com website (https://pragprog.com/book/rwscala). |
You can download the source code used in this book from the book’s web page at pragprog.com.[7] If you’re reading the electronic version of this book, you can click the box above the code excerpts to download that source code directly. Or, if you use the Docker environment, you’ve already got it.
The code is organized by chapter, and within each chapter, the code is organized into individual projects, each with its own folder. Each project is a self-contained codebase, designed to be built by sbt,[8] the standard Scala build tool.
One important note if you’re trying to modify the code: sometimes, for concise presentation, I will not show import statements and outer object Main wrappers in the code printed in the book. For example, have a look at this snippet:
| import scalanative.unsafe._ |
| |
| object Main { |
| def main(args:Array[String]:Unit = { |
| // invoking various functions here |
| ??? |
| } |
| |
| def helperFunction(arg1:Int, arg2:String) = ??? |
| } |
It may be displayed as:
| def main(args:Array[String]:Unit = { |
| // invoking various functions here |
| ??? |
| } |
| |
| def helperFunction(arg1:Int, arg2:String) = ??? |
However, all the code files that you download and use are fully functional and complete. If you’re interested in modifying my examples, definitely start with the files.
Although there’s no one standard for text editors when it comes to Scala, there are many options. Because Scala Native is still relatively new, the support for it is imperfect in complex IDEs. I instead recommend a plain text editor with good support for Scala syntax, like VSCode, Atom, emacs, or vi. Part of the beauty of starting from scratch is that we don’t have to deal with giant Java dependencies with hundreds of classes—our code will be lean enough to write without sophisticated editor assistance. That said, I’ve found that the Metals plugin[9] for VSCode offers a good balance of common-sense help in an unobtrusive way.