[Chapter 02] Getting Started with the Scala Language
Contents
Introduction
This summary is derived from my personal exploration of “Programming in Scala.” Notably, the discussions here are based on Scala 2, which is widely adopted in the industry and supported by a more extensive range of resources compared to Scala 3.
This chapter delves into the practical aspects of Scala programming and is organized into the following sections:
- Scala Interpreter
- Defining Variables
- Crafting Functions
- Writing Scripts
- Iteration Techniques
1. Scala Interpreter
To begin with Scala, utilize the interactive Scala interpreter by entering scala
at a command prompt. This tool allows you to type Scala expressions, which it evaluates and displays the outcomes along with their types.
Example interaction:
scala> 1 + 2
// Output:
res0: Int = 3
Here, ‘res0’ denotes the result identifier, followed by the type (‘Int’), and the computed value (‘3’).
2. Defining Variables
Scala distinguishes between immutable variables (‘vals’) and mutable variables (‘vars’). A ‘val’ is akin to a final variable in Java and cannot be changed once set. A ‘var’, however, can be reassigned. Scala often uses type inference, eliminating the need for explicit type declarations, though you can annotate types explicitly if preferred.
For example:
val greeting = "Hello, world!"
// 'greeting' is inferred as String and cannot be reassigned.
var name = "Scala"
// 'name' can be changed.
3. Crafting Functions
Functions in Scala are declared with the ‘def’ keyword, followed by a list of parameters and the function’s return type. The function body is enclosed in braces.
Example of a basic function:
def max(x: Int, y: Int): Int = {
if (x > y) x
else y
}
// Simplified version without braces:
def max2(x: Int, y: Int) = if (x > y) x else y
Functions can execute actions such as printing to the console and can return ‘Unit’ to indicate no meaningful return value.
4. Writing Scripts
Scala scripts are plain text files containing Scala code. They can be executed directly using the Scala interpreter and can access command-line arguments.
Example:
// File: hello.scala
println("Hello, world, from a script!")
// Run with:
$ scala hello.scala
Scripts support comments and can manipulate strings with the ‘+’ operator.
5. Iteration Techniques
Scala supports both imperative and functional programming styles. For functional iteration, Scala provides constructs like ‘foreach’ and for expressions to handle collections.
Example using ‘foreach’:
args.foreach(arg => println(arg))
// Using for expression:
for (arg <- args)
println(arg)
Each iteration creates a new ‘val’ for the loop variable, ensuring immutability.
Concluding Thoughts
This overview prepares you to engage deeply with Scala, offering a foundation in both syntax and essential programming constructs. As you progress, these concepts will become instrumental in your Scala coding practice.
As we wrap up this chapter, I hope it enriches your understanding and enthusiasm for Scala programming. Feel free to connect and share your thoughts or feedback on LinkedIn.
For further reading, other chapters in this series are available in my reading list.
Resources
Odersky, M., Spoon, L., & Venners, B. (2008). Programming in scala. Artima Inc.