scala cookbook

How to define an `equals` method in a Scala class (object equality)

Table of Contents1 - Solution2 - A Scala `equals` method example3 - Discussion4 - Example 2: A Scala `equals` method with inheritance5 - Implementing hashCode6 - See Also

Scala problem: You want to define an equals method for your class so you can compare object instances to each other.

Back to top

Solution

If you’re new to Scala, a first thing to know is that object instances are compared with ==:

"foo" == "foo"   // true
"foo" == "bar"   // false
"foo" == null    // false
null == "foo"    // false
1 == 1           // true
1 == 2           // false
1d == 1.0d       // true

case class Person(name: String)
Person("Jess") == Person("Jessie")   // false

This is different than Java, which uses == for primitive values and equals for object comparisons.

Writing a book is an adventure. To begin with it is a toy and an amusement. alvin May 2, 2019 - 12:15pm

“Writing a book is an adventure. To begin with it is a toy and an amusement. Then it becomes a mistress, then it becomes a master, then it becomes a tyrant. The last phase is that just as you are about to be reconciled to your servitude, you kill the monster and fling him to the public.”

~ Winston Churchill

I can’t write a book alvin April 7, 2019 - 10:22am

Over time I’ve discovered a number of things that I have no memory of from the years 2014 to 2016, when I was at my sickest with the mast cell disease. Apparently I created this image and wrote these words on August 24, 2015.

I never got a final PDF version of the Scala Cookbook

A funny thing about writing books, or at least writing books with O’Reilly in 2013, is that I never received a final copy of the Scala Cookbook in PDF format. Fortunately I have the original Word docs, which is what they used at the time, so I carry those around on my laptop.

Authors love to hear “thanks”

I doubt that most people know it, but authors always enjoy receiving letters of “thanks” like this one. Writing is often a lonely, solitary business, and to hear that your work has helped other people is always satisfying. If there’s a book you’ve read that you really enjoyed, I encourage you to write the author a brief letter of thanks. They’ll appreciate it more than you’ll know.

A note from reviewing the Scala Cookbook

I'm sitting at Panera Bread, reviewing the first two chapters of the Scala Cookbook. Chapter 1 sucks, someone’s gonna have to do something about that. Chapter 2 isn’t too bad.

A lesson learned from writing the Scala Cookbook

A lesson learned from writing the Scala Cookbook: It’s fun and interesting to work with some professional writers in the editing process, and it’s great to get their feedback. But you also have to be willing to duke it out to keep what’s important. It’s your baby, it’s your name on the front cover.

How to use partially applied functions in Scala

Problem: You want to eliminate repetitively passing variables into a Scala function by (a) passing common variables into the function to (b) create a new function that’s pre-loaded with those values, and then (c) use the new function, passing it only the unique variables it needs.

Solution: The classic example of a partially applied function begins with a simple sum function:

How to define Scala methods that take complex functions as parameters (syntax)

Problem: You want to define a Scala method that takes a function as a parameter, and that function may have one or more input parameters, and may also return a value.

Solution: Following the approach described in the previous recipe, define a method that takes a function as a parameter. Specify the function signature you expect to receive, and then execute that function inside the body of the method.