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

Actor references and paths


When we create an Actor, what we get in response is an ActorRef. This is a reference to our created Actor. Why we might need an ActorRef is to pass throughout the system to other actors as a reference. These references are used for message passing. Every actor that we create has a reference to itself through self.

From within an actor, it's possible to obtain an actor reference of the calling Actor via a method named sender().

We can also give names to actor references. In our case, we named our SimpleActor reference simple-actor:

val simpleActor: ActorRef = system.actorOf(props, "simple-actor")

We also know that these Actors are created in a hierarchical fashion and we can give unique names to actor instances. Hence, these names together make a path for each Actor. The path is unique for each Actor. Our SimpleActor path might look like this:

We can see that due to the hierarchy, we have paths for different actors, because actors must have unique names. We can also...