Skip to content

Update type-classes.md #10869

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

Merged
merged 3 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion docs/docs/reference/contextual/derivation.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ trait Eq[T] {
}

object Eq {
given Eq[Int] {

given Eq[Int] with {
def eqv(x: Int, y: Int) = x == y
}

Expand Down
11 changes: 5 additions & 6 deletions docs/docs/reference/contextual/type-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ trait Monoid[T] extends SemiGroup[T]:
An implementation of this `Monoid` type class for the type `String` can be the following:

```scala
given Monoid[String]:
given Monoid[String] with
extension (x: String) def combine (y: String): String = x.concat(y)
def unit: String = ""
```

Whereas for the type `Int` one could write the following:

```scala
given Monoid[Int]:
given Monoid[Int] with
extension (x: Int) def combine (y: Int): Int = x + y
def unit: Int = 0
```
Expand Down Expand Up @@ -76,9 +76,9 @@ Which could read as follows: "A `Functor` for the type constructor `F[_]` repres
This way, we could define an instance of `Functor` for the `List` type:

```scala
given Functor[List]:
given Functor[List] with
def map[A, B](x: List[A], f: A => B): List[B] =
x.map(f) // List already has a `map` method
x.map(f) // List already has a `map` method
```

With this `given` instance in scope, everywhere a `Functor` is expected, the compiler will accept a `List` to be used.
Expand Down Expand Up @@ -108,11 +108,10 @@ trait Functor[F[_]]:
The instance of `Functor` for `List` now becomes:

```scala
given Functor[List]:
given Functor[List] with
extension [A, B](xs: List[A])
def map(f: A => B): List[B] =
xs.map(f) // List already has a `map` method

```

It simplifies the `assertTransformation` method:
Expand Down