Equality
== in Scala calls equals, and every class inherits one from Any. The inherited version compares references, so a class that stands for a value has to write its own — or let the compiler write it, which is what a case class does. Reference identity has a keyword of its own, eq.
A class with its own equals
Point is a plain class, so it overrides equals and hashCode together: two points with the same coordinates are the same value, and a set of them holds one element, not two. The match is the typed check that keeps the method total.
equality.scala
class Point(val x: Int, val y: Int):
override def equals(other: Any): Boolean =
other match
case that: Point => x == that.x && y == that.y
case _ => false
override def hashCode(): Int = (x, y).hashCode()
case class CasePoint(x: Int, y: Int)
@main def equality(): Unit =
val a = new Point(1, 2)
val b = new Point(1, 2)
println(a == b)
println(a.equals(b))
println(a eq b)
println(Set(a, b).size)
val c = CasePoint(1, 2)
val d = CasePoint(1, 2)
println(c == d)
println(c eq d)
scala-cli run --server=false equality.scala
true
true
false
1
true
false
a == b is true and a eq b is false: the first calls the method, the second asks whether the two names point at one object. The set holds one element because a hash set finds the second point in the bucket of the first, which is why equals without hashCode is a bug that only shows up in a collection.
The case class writes the pair
CasePoint declares nothing but its fields, and the compiler writes equals, hashCode and toString from them. Two instances of the same fields are equal and are still two objects:
A case class is the default for a value in this course for the same reason: the methods that are easy to forget are the ones a reader relies on. A class that needs identity — a connection, a thread, a service — keeps the inherited behaviour, and the difference is written in the declaration.
Comparing unrelated types
Scala 3 refuses to compare two types that have no relationship, which catches a mistake that equals would answer with a silent false:
compare.scala
case class CasePoint(x: Int, y: Int)
@main def compare(): Unit =
println(CasePoint(1, 2) == (1, 2))
mkdir -p out
scalac -color:never -d out compare.scala 2>&1; echo "exit status: $?"
-- [E172] Type Error: compare.scala:4:10 ---------------------------------------
4 | println(CasePoint(1, 2) == (1, 2))
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| Values of types CasePoint and (Int, Int) cannot be compared with == or !=
1 error found
exit status: 1
The error code E172 is the strict equality check of Scala 3: a case class and a tuple are different types, and the compiler asks for an explicit conversion rather than a comparison that can never be true. A comparison inside one hierarchy still compiles, which is the case an equals is written for.
Related
- Case classes: the methods the compiler writes.
- Opaque types: a new type that inherits the equality of its representation.
- Sets and maps: the collections that depend on hashCode.
- Java: equals and hashCode in the language Scala shares them with.
Автор статьи: Arthur Isaev