Skip to content

Commit

Permalink
Address some implicit → given/using changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianhjr committed Feb 18, 2023
1 parent 67bf0a3 commit 389b3d1
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/pages/functors/cats.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ given optionFunctor: Functor[Option] with
Sometimes we need to inject dependencies into our instances.
For example, if we had to define a custom `Functor` for `Future`
(another hypothetical example---Cats provides one in `cats.instances.future`)
we would need to account for the implicit `ExecutionContext` parameter on `future.map`.
we would need to account for the using clause `ExecutionContext` parameter on `future.map`.
We can't add extra parameters to `functor.map`
so we have to account for the dependency when we create the instance:

Expand All @@ -167,7 +167,7 @@ given futureFunctor(using ec: ExecutionContext): Functor[Future] with
Whenever we summon a `Functor` for `Future`,
either directly using `Functor.apply`
or indirectly via the `map` extension method,
the compiler will locate `futureFunctor` by implicit resolution
the compiler will locate `futureFunctor` by given instance resolution
and recursively search for an `ExecutionContext` at the call site.
This is what the expansion might look like:

Expand Down
2 changes: 1 addition & 1 deletion src/pages/functors/contravariant-invariant.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ format(true)

Now define an instance of `Printable` for
the following `Box` case class.
You'll need to write this as an `implicit def`
You'll need to write this as a `given` instance
as described in Section [@sec:type-classes:recursive-implicits]:

```scala mdoc:silent
Expand Down
2 changes: 1 addition & 1 deletion src/pages/monoids/cats.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ are defined directly in the [`cats`][cats.package] package.
the companion object has an `apply` method
that returns the type class instance for a particular type.
For example, if we want the monoid instance for `String`,
and we have the correct implicits in scope,
and we have the correct given instances in scope,
we can write the following:

```scala mdoc:silent
Expand Down
11 changes: 5 additions & 6 deletions src/pages/type-classes/implicits.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ object Json:
w.write(value)
```
```scala mdoc:fail
given secondStringWriter: JsonWriter[String] =
JsonWriterInstances.stringWriter
given secondStringWriter: JsonWriter[String] = stringWriter

Json.toJson("A string")
```
Expand Down Expand Up @@ -183,15 +182,15 @@ When the compiler sees an expression like this:
Json.toJson(Option("A string"))
```

it searches for an implicit `JsonWriter[Option[String]]`.
It finds the implicit method for `JsonWriter[Option[A]]`:
it searches for a given instance `JsonWriter[Option[String]]`.
It finds the given instance for `JsonWriter[Option[A]]`:

```scala mdoc:silent
Json.toJson(Option("A string"))(using optionWriter[String])
```

and recursively searches for a `JsonWriter[String]`
to use as the parameter to `optionWriter`:
for the using clause to `optionWriter`:

```scala mdoc:silent
Json.toJson(Option("A string"))(using optionWriter(using stringWriter))
Expand All @@ -216,7 +215,7 @@ fill in the parameters during given instance resolution.
`given` methods with non-`using` parameters
form a different Scala pattern called an *implicit conversion*.
This is also different from the previous section on `Interface Syntax`,
because in that case the `JsonWriter` is an implicit class with extension methods.
because in that case the `JsonWriter` has extension methods.
Implicit conversion is an older programming pattern
that is frowned upon in modern Scala code.
Fortunately, the compiler will warn you when you do this.
Expand Down
7 changes: 3 additions & 4 deletions src/pages/type-classes/printable.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ trait Printable[A]:
def format(value: A): String
```

Then we define some default *instances* of `Printable`
and package them in `PrintableInstances`:
Then we define some default *instances* of `Printable`:

```scala mdoc:silent
given stringPrintable: Printable[String] with
Expand Down Expand Up @@ -129,10 +128,10 @@ by defining some extension methods to provide better syntax:

2. Define the following extension methods:

- `format` using an implicit `Printable[A]`
- `format` using a `Printable[A]`
and returns a `String` representation of the wrapped `A`;

- `print` using an implicit `Printable[A]` and returns `Unit`.
- `print` using a `Printable[A]` and returns `Unit`.
It prints the wrapped `A` to the console.

3. Use the extension methods to print the example `Cat`
Expand Down
4 changes: 2 additions & 2 deletions src/pages/type-classes/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ We saw the components that make up a type class:

- A `trait`, which is the type class

- Type class instances, which are implicit values.
- Type class instances, which are given instances.

- Type class usage, which uses implicit parameters.
- Type class usage, which utilizes using clauses.


We have also seen the general patterns in Cats type classes:
Expand Down

0 comments on commit 389b3d1

Please sign in to comment.