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
// warning: Option[Int] and Int are unrelated: they will most likely never compare equal
15
-
// res: List[Option[Int]] = List()
16
-
17
14
```
18
15
19
16
Ok, many of you won't have made such a simple mistake as this,
@@ -26,12 +23,11 @@ should have compared `item` to `Some(1)` instead of `1`.
26
23
However, it's not technically a type error because
27
24
`==` works for any pair of objects, no matter what types we compare.
28
25
`Eq` is designed to add some type safety to equality checks
29
-
and work around this problem.
26
+
and work around this problem.#footnote[Scala 3 has it's own solution to this problem, called #href("https://docs.scala-lang.org/scala3/reference/contextual/multiversal-equality.html")[multiversal equality]. It also uses a type class, in this case called `CanEqual`. With the correct imports or compiler flags we can get the compiler to complain if we try to perform an equality check that doesn't make sense. So in practice we don't need `Eq` any more. However it's a simple type class to work with and makes a good introduction to using Cats.]
30
27
31
28
32
29
=== Equality, Liberty, and Fraternity
33
30
34
-
35
31
We can use `Eq` to define type-safe equality
36
32
between instances of any given type:
37
33
@@ -44,7 +40,7 @@ trait Eq[A] {
44
40
}
45
41
```
46
42
47
-
The interface syntax, defined in [`cats.syntax.eq`][cats.syntax.eq],
43
+
The interface syntax, defined in #href("https://www.javadoc.io/doc/org.typelevel/cats-docs_3/latest/cats/syntax/EqSyntax.html")[`cats.syntax.eq`],
48
44
provides two methods for performing equality checks
49
45
provided there is an instance `Eq[A]` in scope:
50
46
@@ -82,13 +78,15 @@ we get a compile error:
82
78
eqInt.eqv(123, "234")
83
79
```
84
80
85
-
We can also import the interface syntax in [`cats.syntax.eq`][cats.syntax.eq]
81
+
We can also import the interface syntax
86
82
to use the `===` and `=!=` methods:
87
83
88
84
```scala mdoc:silent
89
85
import cats.syntax.all.* // for === and =!=
90
86
```
91
87
88
+
Now the syntax methods are available.
89
+
92
90
```scala mdoc
93
91
123 === 123
94
92
123 =!= 234
@@ -126,7 +124,7 @@ the `Option.apply` and `Option.empty` methods from the standard library:
126
124
Option(1) === Option.empty[Int]
127
125
```
128
126
129
-
or using special syntax from [`cats.syntax.option`][cats.syntax.option]:
127
+
or using special syntax from #href("https://www.javadoc.io/doc/org.typelevel/cats-docs_3/latest/cats/syntax/OptionSyntax.html")[`cats.syntax.option`]:
0 commit comments