@@ -13,9 +13,11 @@ Universal equality is convenient but also dangerous since it
13
13
undermines type safety. Say you have an erroneous program where
14
14
a value ` y ` has type ` S ` instead of the expected type ` T ` .
15
15
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
+ ```
19
21
20
22
If all you do with ` y ` is compare it to other values of type ` T ` , the program will
21
23
typecheck but probably give unexpected results.
@@ -26,7 +28,9 @@ restrict the types that are legal in comparisons. The example above
26
28
would not typecheck if an implicit was declared like this for type ` T `
27
29
(or an analogous one for type ` S ` ):
28
30
29
- implicit def eqT: Eq[T, T] = Eq
31
+ ``` scala
32
+ implicit def eqT : Eq [T , T ] = Eq
33
+ ```
30
34
31
35
This definition effectively says that value of type ` T ` can (only) be
32
36
compared 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
36
40
that has any ` Eq ` instance as its type. Here is the definition of class
37
41
` Eq ` and its companion object:
38
42
39
- package scala
40
- import annotation.implicitNotFound
43
+ ``` scala
44
+ package scala
45
+ import annotation .implicitNotFound
41
46
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 ]
44
49
45
- object Eq extends Eq[Any, Any]
50
+ object Eq extends Eq [Any , Any ]
51
+ ```
46
52
47
53
One can have several ` Eq ` instances for a type. For example, the four
48
54
definitions below make values of type ` A ` and type ` B ` comparable with
49
55
each other, but not comparable to anything else:
50
56
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
+ ```
55
63
56
64
(As usual, the names of the implicit definitions don't matter, we have
57
65
chosen ` eqA ` , ..., ` eqBA ` only for illustration).
@@ -66,7 +74,9 @@ There's also a "fallback" instance named `eqAny` that allows comparisons
66
74
over all types that do not themselves have an ` Eq ` instance. ` eqAny ` is
67
75
defined as follows:
68
76
69
- def eqAny[L, R]: Eq[L, R] = Eq
77
+ ``` scala
78
+ def eqAny [L , R ]: Eq [L , R ] = Eq
79
+ ```
70
80
71
81
Even though ` eqAny ` is not declared implicit, the compiler will still
72
82
construct 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
78
88
feature ` strictEquality ` . As for all language features this can be either
79
89
done with an import
80
90
81
- import scala.language.strictEquality
91
+ ``` scala
92
+ import scala .language .strictEquality
93
+ ```
82
94
83
95
or with a command line option ` -language:strictEquality ` .
84
96
@@ -94,11 +106,11 @@ The precise rules for equality checking are as follows.
94
106
a definition of lifting.
95
107
96
108
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 ` .
102
114
103
115
Here _ lifting_ a type ` S ` means replacing all references to abstract types
104
116
in covariant positions of ` S ` by their upper bound, and to replacing
0 commit comments