Book Image

Learning Scala Programming

By : Vikash Sharma
Book Image

Learning Scala Programming

By: Vikash Sharma

Overview of this book

Scala is a general-purpose programming language that supports both functional and object-oriented programming paradigms. Due to its concise design and versatility, Scala's applications have been extended to a wide variety of fields such as data science and cluster computing. You will learn to write highly scalable, concurrent, and testable programs to meet everyday software requirements. We will begin by understanding the language basics, syntax, core data types, literals, variables, and more. From here you will be introduced to data structures with Scala and you will learn to work with higher-order functions. Scala's powerful collections framework will help you get the best out of immutable data structures and utilize them effectively. You will then be introduced to concepts such as pattern matching, case classes, and functional programming features. From here, you will learn to work with Scala's object-oriented features. Going forward, you will learn about asynchronous and reactive programming with Scala, where you will be introduced to the Akka framework. Finally, you will learn the interoperability of Scala and Java. After reading this book, you'll be well versed with this language and its features, and you will be able to write scalable, concurrent, and reactive programs in Scala.
Table of Contents (21 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Container types


We've seen Scala's class hierarchy, hence we are aware of many collection types such as List, Set, and Map. What's different about these types along with types such as Option and Either, is that they all expect you to provide a type and then instantiate. We call List as a container type because it works that way. We use a list to contain elements of a certain data type. Similarly, we can think of an Option as a binary containerized type, as Option can be some value or None. The Either type goes the same way. In Scala, when we create such container types, we tend to use a type parameter to declare and provide a concrete type, such as String,Int, Boolean, and so on when we instantiate. Take a look how Option is declared in Scala (more on Option and Either types in the next chapter):

sealed abstract class Option[+A] extends Product  
   with Serializable 

It takes a type parameter A. It's possible to provide more than one type parameter if your type expects more than one type...