-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Eq docs #1788
Merged
Merged
Add Eq docs #1788
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
layout: docs | ||
title: "Eq" | ||
section: "typeclasses" | ||
source: "kernel/src/main/scala/cats/kernel/Eq.scala" | ||
scaladoc: "#cats.kernel.Eq" | ||
--- | ||
|
||
# Eq | ||
|
||
Eq is an alternative to the standard Java `equals` method. | ||
It is defined by the single method `eqv`: | ||
|
||
```scala | ||
def eqv(x: A, y: A): Boolean | ||
``` | ||
|
||
In Scala it's possible to compare any two values using `==` (which desugars to Java `equals`). | ||
This is because `equals` type signature uses `Any` (Java's `Object`) to compare two values. | ||
This means that we can compare two completely unrelated types without getting a compiler error. | ||
The Scala compiler may warn us in some cases, but not all, which can lead to some weird bugs. | ||
For example this code will raise a warning at compile time: | ||
|
||
|
||
```tut:book:fail | ||
42 == "Hello" | ||
``` | ||
|
||
While this code will compile without a hitch: | ||
|
||
```tut:book | ||
"Hello" == 42 | ||
``` | ||
|
||
Ideally, Scala shouldn't let us compare two types that can never be equal. | ||
|
||
As you can probably see in the type signature of `eqv`, it is impossible to compare two values of different types, | ||
eliminating these types of bugs altogether. | ||
|
||
The `Eq` syntax package also offers some handy symbolic operators: | ||
|
||
```tut:book | ||
import cats.implicits._ | ||
|
||
1 === 1 | ||
|
||
"Hello" =!= "World" | ||
``` | ||
|
||
Implementing `Eq` instances yourself for every data type might seem like huge drawback compared to only slight gains of typesafety. | ||
Fortunately for us, we have two great options. One option is to use inbuilt helper functions. | ||
Another option is to use a small library called [kittens](https://github.com/milessabin/kittens), which can derive a lot of type class instances for our data types including `Eq`. | ||
|
||
The first option using `Eq.fromUniversalEquals` only defers to `==` and works like this: | ||
|
||
```tut:book | ||
import cats.kernel.Eq | ||
import cats.implicits._ | ||
|
||
|
||
case class Foo(a: Int, b: String) | ||
|
||
|
||
implicit val eqFoo: Eq[Foo] = Eq.fromUniversalEquals | ||
|
||
|
||
Foo(10, "") === Foo(10, "") | ||
``` | ||
|
||
|
||
For an example using Kittens check out the [kittens repo](https://github.com/milessabin/kittens). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could mention
fromUniversalEquals
which should work fine for case classes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure this should go in the documentation, but I recently found this useful:
unless I'm missing something (and if so, please let me know!)
fromUniversalEqual
should work fine with anyProduct
. This makes it incredibly easy for the users to provide a reasonableEq
instance for every case class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's pretty interesting, seems like it would work for all case classes! Any known downsides?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not that I know. We're using it in a project since a few weeks and so far so good.
That said, I'm far from being an expert :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it's worth mentioning in the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added :)