Scala at Light Speed, Part 1: The Essentials
Scala at Light Speed, Part 1: The Essentials
Learn Scala fast in this installment on learning Scala essentials, including types, values, and more.
Join the DZone community and get the full member experience.Join For Free
This article series is for busy programmers who want to learn Scala fast — in about 2 hours or less. This article series is the written version of Rock the JVM's Scala at Light Speed mini-course, which you can find for free on YouTube or on the Rock the JVM website in video form.
You may also like: Kotlin Vs. Scala (Vs. Java)
This series will assume that:
- You have 2 hours of focused time to dedicate to the series/ about 20 minutes per article
The contents of this article are available in video here or in the above embedded video.
All you need to know about Scala, for now, is:
- It's based on the JVM, so you can use all libraries and tools in the Java ecosystem.
- It blends object-orientation and functional programming.
You'll need a good IDE to try out the code in this series. I recommend IntelliJ IDEA with the Scala plugin installed. The setup is straightforward — at first launch, the setup will ask which plugins you want installed, and Scala is one of the featured ones; just check it, and proceed.
Next, go to this GitHub repository and download the support code for this mini-course. If you know how to use Git, clone the repository and run
git checkout start to get the code to the initial state. If you don't know how to use Git, download the initial state of the repository as a zip file from GitHub. Then simply open this folder in IntelliJ.
src/main/scala, in the package
com.rockthejvm, you will find an application called
Playground.scala where I will invite you to fiddle with any Scala code you find useful. It's the standard hello-world and looks like this:
If you like, you can right-click on the code and click Run to display that message to the console. Don't worry about what anything means at the moment.
Values and Types
In Scala, we work with values:
The structure is:
val, name of the value,
= and then the right-hand side.
Values are constants. They cannot be changed/reassigned. The above is the same as the following Java/C++ code:
In Scala, we work with values and compose these values to obtain new values.
: YourType bit where you specify the type can be often omitted. The compiler can figure out the type of your value by inspecting the right hand side. 42 is an integer, so your value is an Int. You can also write:
... without the type, and you'll be fine. Common types in Scala are similar to what you see in other languages: Boolean, Int, Long, Float, Double, Char, String.
Strings in particular are similar to what you see in other languages, but with some extra niceties:
- We did not add the type to any value in the above snippet. The compiler is smart.
- Strings can be concatenated with +.
- The third value has an
sbefore the quote. That's an s-interpolated string, in which you can inject another value with the
In Scala, we work with values and we compose them to obtain other values. The composition structures are expressions, and they're exactly what you expect.
Expressions are structures that can be reduced to a value. Unlike other languages where we think in terms of instructions, i.e. "do this, do that, and do this as long as this is true," in Scala, we think in values and expressions that combine these values.
If-structures are expressions as well:
This is an expression that takes the value 56 or 999. In C-like languages, we have this equivalent ternary operator:
meaningOfLife > 43 ? 56 : 999, but in Scala, it's much more readable because you can chain if-expressions in endless if/else if structures without the risk of misunderstanding any logic.
Other notable expressions are code blocks:
The rules of code blocks are:
- They act as a scope, i.e. you can define local values, classes, functions that are not visible outside.
- Their value is the value of their last expression (and so they are expressions themselves).
With some technicalities aside, we define functions as:
So we have:
- function name
- arguments in the form of
- then a single expression the function will return.
An important tool in functional programming is the use of recursion. Because we are not using variables or loops in Scala, we think in terms of recursion instead. If for some reason you could take only one lesson away from this article, it's this:
In Scala, we don't think in terms of loops. We think in terms of recursion.
The Unit Type
Finally, I'll end this article with the type of expressions that don't return any meaningful value, i.e. the equivalent of "void" functions in other languages. Examples: printing something on-screen, writing to a file, sending data through a socket.
A Scala example is the
println function, which returns Unit, the equivalent of "void" in other languages. Unit is a type containing a single value denoted
() and is returned by every "void" function. The Unit value
() is not used (or useful) per se. Expressions returning Unit (like printing) are called side effects, because they have nothing to do with computing meaningful values. In pure functional programming, we tend to keep side effects to a minimum.
In the next article, we will talk about Scala as an object-oriented language.
Stay tuned for our next installment where we look at object-orientation in Scala. You don't want to miss it!
Opinions expressed by DZone contributors are their own.