You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/multiversal-equality.md
+11-4Lines changed: 11 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -160,7 +160,7 @@ One particular feature of the `Eql` type is that it takes _two_ type parameters,
160
160
implementations of an equality type class take only a single type parameter which represents the common type of _both_ operands. One type parameter is simpler than two, so why go through the additional complication? The reason has to do with the fact that, rather than coming up with a type class where no operation existed before,
161
161
we are dealing with a refinement of pre-existing, universal equality. It's best illustrated through an example.
162
162
163
-
Say you want to come up with a safe version of the `contains` method on `List[T]`. The current definition of `contains` in the standard library is:
163
+
Say you want to come up with a safe version of the `contains` method on `List[T]`. The original definition of `contains` in the standard library was:
164
164
```scala
165
165
classList[+T] {
166
166
...
@@ -175,7 +175,8 @@ does not work, since it refers to the covariant parameter `T` in a nonvariant co
175
175
```scala
176
176
defcontains[U>:T](x: U):Boolean
177
177
```
178
-
This generic version of `contains` admits exactly the same applications as the `contains(x: Any)` definition we started with. But we can make it more useful (i.e. restrictive) by adding an `Eql` parameter:
178
+
This generic version of `contains` is the one used in the current version of
179
+
admits exactly the same applications as the `contains(x: Any)` definition we started with. But we can make it more useful (i.e. restrictive) by adding an `Eql` parameter:
@@ -201,8 +202,14 @@ unusable for all types that have not yet declared an `Eql1` instance, including
201
202
types coming from Java. This is clearly unacceptable. It would lead to a situation where,
202
203
rather than migrating existing libraries to use safe equality, the only upgrade path is to have parallel libraries, with the new version only catering to types deriving `Eql1` and the old version dealing with everything else. Such a split of the ecosystem would be very problematic, which means the cure is likely to be worse than the disease.
203
204
204
-
For these reasons, it looks like a two-parameter type class is the only way forward because it can take the existing ecosystem where it is and migrate it towards a future
205
-
where more and more code uses safe equality.
205
+
For these reasons, it looks like a two-parameter type class is the only way forward because it can take the existing ecosystem where it is and migrate it towards a future where more and more code uses safe equality.
206
+
207
+
In applications where `-language:strictEquality` is the default one could also introduce a one-parameter type alias such as
208
+
```scala
209
+
typeEq[-T] =Eql[T, T]
210
+
```
211
+
Operations needing safe equality could then use this alias instead of the two-parameter `Eql` class. But it would only
212
+
work under `-language:strictEquality`, since otherwise the universal `Eq[Any]` instance would be available everywhere.
206
213
207
214
208
215
More on multiversal equality is found in a [blog post](http://www.scala-lang.org/blog/2016/05/06/multiversal-equality.html)
0 commit comments