Skip to content

Commit 6e1781c

Browse files
committed
Merge pull request scala#431 from odersky/blog-mulitversal-equality
New blog post: universal equality
2 parents e85cd18 + 3bf72c4 commit 6e1781c

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
layout: blog
3+
post-type: blog
4+
by: Martin Odersky
5+
title: Multiversal Equality for Scala
6+
disqus: true
7+
---
8+
9+
I have been working recently on making equality tests using `==` and
10+
`!=` safer in Scala. This has led to a [Language Enhancement
11+
Proposal](https://github.com/lampepfl/dotty/issues/1247). This blog
12+
post gives an overview of the proposal.
13+
14+
## Why Change Equality?
15+
16+
Scala prides itself of its strong static type system. Its type discipline is particularly useful when it comes to refactoring. Indeed, it's possible to write programs in such a way that refactoring problems show up with very high probability as type errors. This is essential for being able to refactor with the confidence that nothing will break. And the ability to do such refactorings is jn turn very important for keeping code bases from rotting.
17+
18+
Of course, getting such a robust code base requires the cooperation of the developers. They should avoid type `Any`, casts, stringly typed logic, and more generally any operation over loose types that do not capture the important properties of a value. Unfortunately, there is one area in Scala where such loose types are very hard to avoid: That's equality. Comparisons with `==` and `!=` are _universal_. They compare any two values, no matter what their types are. This causes real problems for writing code and more problems for refactoring it.
19+
20+
For instance, one might want to introduce a proxy for some data structure so that instead of accessing the data structure directly one goes through the proxy. The proxy and the underlying data would have different types. Normally this should be an easy refactoring. If one passes by accident a proxy for the underlying type or _vice versa_ the type checker will flag the error. However, if one accidentally compares a proxy with the underlying type using `==` or a pattern match, the program is still valid, but will just always say `false`. This is a real worry in practice. I recently abandoned a desirable extensive refactoring because I feared that it would be too hard to track down such errors.
21+
22+
## Where Are We Today?
23+
24+
The problems of universal equality in Scala are of course well
25+
known. Some libraries have tried to fix it by adding another equality
26+
operator with more restricted typing. Most often this safer equality
27+
is written `===`. While `===` is certainly useful, I am not a fan of
28+
adding another equality operator to the language and core
29+
libraries. It would be much better if we could fix `==` instead. This
30+
would be both simpler and would catch all potential equality problems
31+
including those related to pattern matching.
32+
33+
How can `==` be fixed? It looks much harder to do this than adding an
34+
alternate equality operator. First, we have to keep backwards
35+
compatibility. The ability to compare everything to everything is by
36+
now baked into lots of code and libraries.
37+
Second, with just one equality operator
38+
we need to make this operator work in all cases where it makes
39+
sense. An alternative `===` operator can choose to refuse some
40+
comparisons that should be valid because there's always `==`
41+
to fall back to. With a unique `==` operator we do not have this
42+
luxury.
43+
44+
The current status in Scala is that the compiler will give warnings
45+
for _some_ comparisons that are always `false`. But the coverage is
46+
weak. For instance this will give a warning:
47+
48+
scala> 1 == "abc"
49+
<console>:12: warning: comparing values of types Int and String using `==' will always yield false
50+
51+
But this will not:
52+
53+
scala> "abc" == 1
54+
res2: Boolean = false
55+
56+
There are also cases where a warning is given for a valid equality
57+
test that actually makes sense because the result could be `true`. In
58+
summary, the current checking catches some obvious bugs, which is
59+
nice. But it is far too weak and fickle to be an effective refactoring
60+
aid.
61+
62+
63+
## What's Proposed?
64+
65+
I believe to do better, we need to enlist the cooperation of
66+
developers. Ultimately it's the developer who provides implementations
67+
of equality methods and who is therefore best placed to characterize
68+
which equalities make sense. Sometimes this characterization can be
69+
involved. For instance, an `Int` can be compared to other primitive
70+
numeric values or to instances of type `java.lang.Number` but any other
71+
comparison will always yield `false`. Or, it makes sense to compare
72+
two `Option` values if and only if it makes sense to compare the optional
73+
element values.
74+
75+
The best known way to characterize such relationships is with type
76+
classes. Implicit values of a trait `Eq[T, U]` can capture the
77+
property that values of type `T` can be compared to values of type
78+
`U`. Here's the definition of `EQ`
79+
80+
package scala
81+
82+
trait Eq[-T, -U]
83+
84+
That is, `Eq` is a pure marker trait two type parameters and without
85+
any members. Developers can define equality classes by giving
86+
implicit `Eq` instances. Here is a simple one:
87+
88+
implicit def eqString: Eq[String, String] = Eq
89+
90+
This states that strings can be only compared to strings, not to values of other types.
91+
Here's a more complicated `Eq` instance:
92+
93+
implicit def eqOption[T, U](implicit _eq: Eq[T, U]): Eq[Option[T], Option[U]] = Eq
94+
95+
This states that `Option` values can be compared if their elements can be compared.
96+
97+
It's foreseen that such `Eq` instances can be generated automatically. If we add
98+
an annotation `@equalityClass` to `Option` like this
99+
100+
`equalityClass` class Option[+T] { ... }
101+
102+
then the `eqOption` definition above would be generated automatically in `Option`'s companion object.
103+
104+
Given a set of `Eq` instances, the idea is that the Scala
105+
compiler will check every time it encounters a _potentially
106+
problematic_ comparison between values of types `T` and `U` that there
107+
is an implicit instance of `Eq[T, U]`. A comparison is _potentially
108+
problematic_ if it is between incompatible types. As long as `T <: U`
109+
or `U <: T` the equality could make sense because both sides can
110+
potentially be the same value.
111+
112+
So this means we still keep universal equality as it is in Scala now
113+
- we don't have a choice here anyway, because of backwards
114+
compatibility. But we render it safe by checking that for each
115+
comparison the corresponding `Eq` instance exists.
116+
117+
What about types for which no `Eq` instance exists? To maintain
118+
backwards compatibility, we allow comparisons of such types as well,
119+
by means of a fall-back `eqAny` instance. But we do not allow comparisons
120+
between types that have an `Eq` instance and types that have none.
121+
Details are explained in the
122+
[proposal](https://github.com/lampepfl/dotty/issues/1247).
123+
124+
## Properties
125+
126+
Here are some nice properties of the proposal
127+
128+
1. It is _opt-in_. To get safe checking, developers have to annotate classes that should
129+
allow comparisons only between their instances with `@equalityClass`, or they have to define implicit
130+
`Eq` instances by hand.
131+
2. It is backwards compatible. Without `@equalityClass` annotations equality works as before.
132+
3. It carries no run-time cost compared to universal equality. Indeed the run-time behavior of
133+
equality is not affected at all.
134+
4. It has no problems with parametricity, variance, or bottom types.
135+
5. Depending on the actual `Eq` instances given, it can be very precise. That is,
136+
no comparisons that might yield `true` need to be rejected, and most comparisons that
137+
will always yield `false` are in fact rejected.
138+
139+
The scheme effectively leads to a partition of the former universe of
140+
types into sets of types. Values with types in the same partition can
141+
be compared among themselves but values with types in different
142+
partitions cannot.
143+
144+
An `@equalityClass` annotation on a type creates a new partition. All
145+
types that do not have any `Eq` instances (except `eqAny`, that is)
146+
form together another partition.
147+
148+
So instead of a single _universe_ of values that can be compared to
149+
each other we get a _multiverse_ of partitions. Hence the name of the
150+
proposal: **Multiversal Equality**.

0 commit comments

Comments
 (0)