Skip to Content
Scala Cookbook, 2nd Edition
book

Scala Cookbook, 2nd Edition

by Alvin Alexander
August 2021
Intermediate to advanced
799 pages
17h 41m
English
O'Reilly Media, Inc.
Content preview from Scala Cookbook, 2nd Edition

Chapter 7. Objects

Continuing the domain modeling chapters, the word object has a dual meaning in Scala. As with Java, you use the name to refer to an instance of a class, but in Scala object is much more well known as a keyword. This chapter demonstrates both meanings of the word.

The first two recipes look at an object as an instance of a class. They show how to cast objects from one type to another and demonstrate the Scala equivalent of Java’s .class approach.

The remaining recipes demonstrate how the object keyword is used for other purposes. In the most basic use, Recipe 7.3 shows how to use it to create singletons. Recipe 7.4 demonstrates how to use companion objects as a way to add static members to a class, and then Recipe 7.5 shows how to use apply methods in companion objects as an alternative way to construct class instances.

After those recipes, Recipe 7.6 shows how to create a static factory using an object, and Recipe 7.7 demonstrates how to combine one or more traits into an object in a process that’s technically known as reification. Finally, pattern matching is a very important Scala topic, and Recipe 7.8 demonstrates how to write an unapply method in a companion object so your classes can be used in match expressions.1

7.1 Casting Objects

Problem

You need to cast an instance of a class from one type to another, such as when creating objects dynamically.

Solution

In the following example I’ll work with the Sphinx-4 speech recognition library, which works ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Scala Cookbook

Scala Cookbook

Alvin Alexander
Programming Scala, 2nd Edition

Programming Scala, 2nd Edition

Dean Wampler, Alex Payne
Functional Programming in Scala, Second Edition

Functional Programming in Scala, Second Edition

Runar Bjarnason, Michael Pilquist, Paul Chiusano

Publisher Resources

ISBN: 9781492051534Errata Page