Book Image

Scala Programming Projects

By : Mikael Valot, Nicolas Jorand
Book Image

Scala Programming Projects

By: Mikael Valot, Nicolas Jorand

Overview of this book

Scala Programming Projects is a comprehensive project-based introduction for those who are new to Scala. Complete with step-by-step instructions and easy-to-follow tutorials that demonstrate best practices when building applications, this Scala book will have you building real-world projects in no time. Starting with the fundamentals of software development, you’ll begin with simple projects, such as developing a financial independence calculator, and then advance to more complex projects, such as a building a shopping application and a Bitcoin transaction analyzer. You’ll explore a variety of Scala features, including its OOP and FP capabilities, and learn how to write concise, reactive, and concurrent applications in a type-safe manner. You’ll also understand how to use libraries such as Akka and Play. Furthermore, you’ll be able to integrate your Scala apps with Kafka, Spark, and Zeppelin, along with deploying applications on a cloud platform. By the end of the book, you’ll have a firm foundation in Java programming that’ll enable you to solve a variety of real-world problems, and you’ll have built impressive projects to add to your professional portfolio.
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Using the basic features


In this section, and in the rest of this book, we will highlight some key shortcuts in italics. I strongly encourage you to use and remember these shortcuts. They save a tremendous amount of time and will keep you focused on the task at hand. If you cannot remember a shortcut, you can use the mother of all shortcuts, Ctrl + Shift + A (Windows/Linux) or cmd + shift + A (macOS), and type the name of the action you are looking for.

If you are using IntelliJ for the first time, I find it useful to display all tool buttons. Go to the View menu, and check Toolbar and Tool buttons.

SBT synchronization

Now, let's have a look at our build configuration. SBT (short for Simple Build Tool) is the de facto build tool in the Scala community. Double-click on build.sbt:

name := 'scala_fundamentals"

version := "0.1"

scalaVersion := "2.12.4"

This file describes how SBT will compile, test, and deploy our project. For now, it is fairly simple.

One important thing to keep in mind is that IntelliJ manages its own set of files to define a project structure. They are located in the .idea directory of your project. Whenever you change build.sbt, IntelliJ has to interpret the changes and translate them.

For instance, If I change the Scala version to 2.12.3 and save (Ctrl + S or cmd + S), IntelliJ will propose to synchronize the changes or enable autoimport:

On a small project, it is ok to use autoimport, but on a large one, it can be a bit annoying. The synchronization can take time and it might kick off too often.

When you program in Scala using IntelliJ, you therefore have two ways of compiling your project:

  • SBT, in which case you would only use IntelliJ as an advanced text editor
  • IntelliJ

You could, in theory, mix and match: start building with SBT and continue with IntelliJ or the other way around. However, I strongly discourage you to do so, as you may get some unexpected compilation errors. When you want to switch to one tool or the other, it is best to clean all compiled files first.

We will further expand on SBT later in this book, but for now, we are only going to use IntelliJ's own build.

Build

The project has been created and ready to be built. The build process does the following:

  • Compiles the source files present at the source path and the test path
  • Copies any resource files needed in the output path
  • Reports any errors/warnings in the Message tool window

There are two ways to build the project:

  • If you want to build your project incrementally, go to Build | Build Project (Ctrl + F9 or cmd + F9)
  • If you want to delete all files and rebuild everything, go to Build | Rebuild All

As we do not have a source yet, the build is fast and no errors should appear in the Message tool window.