@@ -13,9 +13,11 @@ Universal equality is convenient but also dangerous since it
1313undermines type safety. Say you have an erroneous program where
1414a value ` y ` has type ` S ` instead of the expected type ` T ` .
1515
16- val x = ... // of type T
17- val y = ... // of type S, but should be T
18- x == y // typechecks, will always yield false
16+ ``` scala
17+ val x = ... // of type T
18+ val y = ... // of type S, but should be T
19+ x == y // typechecks, will always yield false
20+ ```
1921
2022If all you do with ` y ` is compare it to other values of type ` T ` , the program will
2123typecheck but probably give unexpected results.
@@ -26,7 +28,9 @@ restrict the types that are legal in comparisons. The example above
2628would not typecheck if an implicit was declared like this for type ` T `
2729(or an analogous one for type ` S ` ):
2830
29- implicit def eqT: Eq[T, T] = Eq
31+ ``` scala
32+ implicit def eqT : Eq [T , T ] = Eq
33+ ```
3034
3135This definition effectively says that value of type ` T ` can (only) be
3236compared with ` == ` or ` != ` to other values of type ` T ` . The definition
@@ -36,22 +40,26 @@ the negation of `equals`. The right hand side of the definition is a value
3640that has any ` Eq ` instance as its type. Here is the definition of class
3741` Eq ` and its companion object:
3842
39- package scala
40- import annotation.implicitNotFound
43+ ``` scala
44+ package scala
45+ import annotation .implicitNotFound
4146
42- @implicitNotFound("Values of types ${L} and ${R} cannot be compared with == or !=")
43- sealed trait Eq[-L, -R]
47+ @ implicitNotFound(" Values of types ${L} and ${R} cannot be compared with == or !=" )
48+ sealed trait Eq [- L , - R ]
4449
45- object Eq extends Eq[Any, Any]
50+ object Eq extends Eq [Any , Any ]
51+ ```
4652
4753One can have several ` Eq ` instances for a type. For example, the four
4854definitions below make values of type ` A ` and type ` B ` comparable with
4955each other, but not comparable to anything else:
5056
51- implicit def eqA : Eq[A, A] = Eq
52- implicit def eqB : Eq[B, B] = Eq
53- implicit def eqAB: Eq[A, B] = Eq
54- implicit def eqBA: Eq[B, A] = Eq
57+ ``` scala
58+ implicit def eqA : Eq [A , A ] = Eq
59+ implicit def eqB : Eq [B , B ] = Eq
60+ implicit def eqAB : Eq [A , B ] = Eq
61+ implicit def eqBA : Eq [B , A ] = Eq
62+ ```
5563
5664(As usual, the names of the implicit definitions don't matter, we have
5765chosen ` eqA ` , ..., ` eqBA ` only for illustration).
@@ -66,7 +74,9 @@ There's also a "fallback" instance named `eqAny` that allows comparisons
6674over all types that do not themselves have an ` Eq ` instance. ` eqAny ` is
6775defined as follows:
6876
69- def eqAny[L, R]: Eq[L, R] = Eq
77+ ``` scala
78+ def eqAny [L , R ]: Eq [L , R ] = Eq
79+ ```
7080
7181Even though ` eqAny ` is not declared implicit, the compiler will still
7282construct an ` eqAny ` instance as answer to an implicit search for the
@@ -78,7 +88,9 @@ if this is of no concern one can disable `eqAny` by enabling the language
7888feature ` strictEquality ` . As for all language features this can be either
7989done with an import
8090
81- import scala.language.strictEquality
91+ ``` scala
92+ import scala .language .strictEquality
93+ ```
8294
8395or with a command line option ` -language:strictEquality ` .
8496
@@ -94,11 +106,11 @@ The precise rules for equality checking are as follows.
94106 a definition of lifting.
95107
96108 2 . The usual rules for implicit search apply also to ` Eq ` instances,
97- with one modification: An instance of ` scala.Eq.eqAny[T, U] ` is
98- constructed if neither ` T ` nor ` U ` have a reflexive ` Eq `
99- instance themselves. Here, a type ` T ` has a reflexive ` Eq `
100- instance if the implicit search for ` Eq[T, T] ` succeeds
101- and constructs an instance different from ` eqAny ` .
109+ with one modification: If the ` strictEquality ` feature is not enabled,
110+ an instance of ` scala.Eq.eqAny[T, U] ` is constructed if neither ` T `
111+ nor ` U ` have a reflexive ` Eq ` instance themselves. Here, a type ` T `
112+ has a reflexive ` Eq ` instance if the implicit search for ` Eq[T, T] `
113+ succeeds and constructs an instance different from ` eqAny ` .
102114
103115 Here _ lifting_ a type ` S ` means replacing all references to abstract types
104116 in covariant positions of ` S ` by their upper bound, and to replacing
0 commit comments