{{announcement.body}}
{{announcement.title}}

Scala at Light Speed, Part 1: The Essentials

DZone 's Guide to

Scala at Light Speed, Part 1: The Essentials

Learn Scala fast in this installment on learning Scala essentials, including types, values, and more.

· Java Zone ·
Free Resource

Learn Scala at the speed of light!

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 some programming experience with other languages — we'll often make references to C, C++, Java, Python, and JavaScript so that you can easily relate to the concepts
  • You have 2 hours of focused time to dedicate to the series/ about 20 minutes per article


Getting Started

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.

Over at 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:

Scala


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.

The Essentials

Values and Types

In Scala, we work with values:

Scala


The structure is: val, name of the value, : YourType, = 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:

Java


In Scala, we work with values and compose these values to obtain new values.

The : 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:

Scala


... 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:

Scala


Notice:

  1. We did not add the type to any value in the above snippet. The compiler is smart.
  2. Strings can be concatenated with +.
  3. The third value has an s before the quote. That's an s-interpolated string, in which you can inject another value with the $ sign.

Expressions

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.

Scala


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:

Scala


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:

Scala


The rules of code blocks are:

  1. They act as a scope, i.e. you can define local values, classes, functions that are not visible outside.
  2. Their value is the value of their last expression (and so they are expressions themselves).

Functions

With some technicalities aside, we define functions as:

Scala


So we have:

  • def
  • function name
  • arguments in the form of arg: Type
  • : ReturnType
  • =
  • 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.

As this article assumes you have some programming experience, we'll also assume you know any iterative code has an equivalent recursive version. This particular lesson is hard to internalize in practice because most programmers started with an imperative language like C, Java, Python or JavaScript.

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...

Stay tuned for our next installment where we look at object-orientation in Scala. You don't want to miss it!

Further Reading

Clean Code Best Practices in Scala

A Journey With Scala

[DZone Refcard] Getting Started With Scala

Topics:
scala ,functional programming

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}