Skip to content
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 7 commits into from
Sep 21, 2017
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions docs/src/main/tut/typeclasses/eq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
layout: docs
title: "Eq"
section: "typeclasses"
source: "kernel/src/main/scala/cats/kernel/Eq.scala"
scaladoc: "#cats.kernel.Eq"
---

# Eq

Show is an alternative to the standard Java `equals` method.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Show/Eq ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whooops!

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.
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

Copy link
Contributor

@gabro gabro Aug 9, 2017

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:

implicit def productEq[A <: Product]: Eq[A] = Eq.fromUniversalEquals

unless I'm missing something (and if so, please let me know!) fromUniversalEqual should work fine with any Product. This makes it incredibly easy for the users to provide a reasonable Eq instance for every case class.

Copy link
Member Author

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?

Copy link
Contributor

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 :)

Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added :)

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, "")
```

You can even go one step further and make use of the fact, that every case class will extend `scala.Product`, by creating an `Eq` instance for it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this mechanism is probably good enough for all Product, so it doesn't limit to case classes. How about:

You can even go one step further by making this implicitly available for all scala.Product (which include case classes).

implicit def eqProduct[A <: Product]: Eq[A] = Eq.fromUniversalEquals

This means, you'll get an instance for all your case classes, but you'll have to define them for regular classes and objects yourself.
```tut:book

case class Bar(a: Double, b: Int)

implicit def eqProduct[A <: Product]: Eq[A] = Eq.fromUniversalEquals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to disagree with a couple of other commenters, but personally I wouldn't be too inclined to include this example in the docs. A couple of significant difference between this and kittens derivation is that kittens will only derive the Eq instance when all types within the case class have Eq instances, and it will be consistent with those individual Eq instances, while this relies on universal equality so it won't necessarily.

I like the fromUniversalEquals method and use it on occasion, but I think it's a big leap to add this instance into implicit scope for all Product types. I don't want to be a stick in the mud though, so if others want to go forward with this in the docs, that's okay.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a really good point. It didn't come to my mind that fromUniversalEquals wont recursively use Eq for fields. Agree we should remove this example usage.


Bar(2, 0) === Bar(2, 0)
```


For an example using Kittens check out the [kittens repo](https://github.com/milessabin/kittens).