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

Using cats.syntax.all instead of cats.implicits in the docs #4026

Merged
merged 1 commit into from
Oct 26, 2021
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
8 changes: 4 additions & 4 deletions docs/src/main/mdoc/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The easiest approach to Cats imports is to import everything that's commonly nee
```scala mdoc:silent
import cats._
import cats.data._
import cats.implicits._
import cats.syntax.all._
```

This should be all that you need, but if you'd like to learn more about the details of imports than you can check out the [import guide](typeclasses/imports.html).
Expand Down Expand Up @@ -241,7 +241,7 @@ All other symbols can be imported with `import cats.implicits._`
| `f <<< g` | Arrow compose | | `Compose[F[_, _]]` | `compose(f: F[B, C], g: F[A, B]): F[A, C]` |
| `f >>> g` | Arrow andThen | | `Compose[F[_, _]]` | `andThen(f: F[B, C], g: F[A, B]): F[A, C]` |
| `f &&& g` | Arrow merge | | `Arrow[F[_, _]]` | `merge[A, B, C](f: F[A, B], g: F[A, C]): F[A, (B, C)]` |
| `f -< g` | Arrow combine and bypass | | `Arrow[F[_, _]]` | `combineAndByPass[A, B, C](f: F[A, B], g: F[B, C]): F[A, (B, C)]` |
| `f -< g` | Arrow combine and bypass | | `Arrow[F[_, _]]` | `combineAndByPass[A, B, C](f: F[A, B], g: F[B, C]): F[A, (B, C)]` |
| `F ~> G` | natural transformation | | `FunctionK[F[_], G[_]]` | `FunctionK` alias |
| `F :<: G` | injectK | | `InjectK[F[_], G[_]]` | `InjectK` alias |
| `F :≺: G` | injectK | | `InjectK[F[_], G[_]]` | `InjectK` alias |
Expand All @@ -265,14 +265,14 @@ The Сats community welcomes and encourages contributions, even if you are compl

See the [contributing guide]({{ site.baseurl }}/contributing.html) for more information.

## <a id="ammonite" href="#ammonite"></a>How to try Cats in a REPL?
## <a id="ammonite" href="#ammonite"></a>How to try Cats in a REPL?

The easiest way is probably using [Ammonite-REPL](http://ammonite.io/). Install it following the instructions there. Then in the amm console you can type in
```scala
// interp.configureCompiler(_.settings.YpartialUnification.value = true) // If using scala 2.11 or 2.12
import $ivy.`org.typelevel::cats-core:2.1.1`, cats._, cats.data._, cats.implicits._
```
Or if you want, you can add these lines to `~/.ammonite/predef.sc` so that they are enabled every ammonite session.
Or if you want, you can add these lines to `~/.ammonite/predef.sc` so that they are enabled every ammonite session.

## <a id="monad-transformer-variance" href="#monad-transformer-variance"></a>Why aren't monad transformers like `OptionT` and `EitherT` covariant like `Option` and `Either`?

Expand Down
50 changes: 21 additions & 29 deletions docs/src/main/mdoc/typeclasses/imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,32 @@ section: "imports"

The easiest approach to Сats imports is to import everything that's commonly needed:

```scala mdoc:silent
```scala mdoc:reset:silent
import cats._
import cats.data._
import cats.implicits._
import cats.syntax.all._
```

The `cats._` import brings in quite a few [type classes](http://typelevel.org/cats/typeclasses.html) (similar to interfaces) such as [Monad](http://typelevel.org/cats/typeclasses/monad.html), [Semigroup](http://typelevel.org/cats/typeclasses/semigroup.html), and [Foldable](http://typelevel.org/cats/typeclasses/foldable.html). Instead of the entire `cats` package, you can import only the types that you need, for example:

```scala mdoc:silent
```scala mdoc:reset:silent
import cats.Monad
import cats.Semigroup
import cats.Foldable
```

The `cats.data._`, import brings in data structures such as [Validated](http://typelevel.org/cats/datatypes/validated.html) and [State](http://typelevel.org/cats/datatypes/state.html). Instead of the entire `cats.data` package, you can import only the types that you need, for example:

```scala mdoc:silent
```scala mdoc:reset:silent
import cats.data.Validated
import cats.data.State
```

The `cats.implicits._` import does a couple of things. Firstly, it brings in implicit type class instances for standard library types - so after this import you will have `Monad[List]` and `Semigroup[Int]` instances in implicit scope. Secondly, it adds syntax enrichment onto certain types to provide some handy methods such as right-biased `Either` combinators:
The `cats.syntax.all._` import adds syntax enrichment onto certain types to provide some handy methods such as right-biased `Either` combinators:

```scala mdoc:reset
import cats.syntax.all._

```scala mdoc
// Сats adds right-biased combinators to the standard library's Either
val e: Either[String, Int] = Right(3)
e.map(_ + 1)
Expand All @@ -40,39 +42,29 @@ val o: Option[String] = None
o.orEmpty
```

If you'd like to import à la carte, you can do so, by importing from `cats.instances` for the type class instances and `cats.syntax` for syntax enrichment.
For example, if you'd like to import the `Monoid` instance for `String` and the corresponding syntax:
If you'd like to import à la carte, you can do so, by importing from `cats.syntax` for syntax enrichment.
For example, if you'd like to import the `Semigroup` syntax:

```scala mdoc
import cats.instances.string._
```scala mdoc:reset
import cats.syntax.semigroup._

"Hello, " |+| "World!"
```
The first import pulls the `Semigroup` instance for String into the scope, while the second import adds the `|+|` syntax.

You can also import all syntax or all instances by importing `cats.syntax.all._` or `cats.instances.all._` respectively.

For data types included in cats (i.e. data structure from the `cats.data` package), all type class instances are bundled with their implementation and therefore do not need to be imported separately.
For example, if we wanted to import `NonEmptyList` from the `cats.data` package and use its `SemigroupK` instance, we would not need to specifically import the instance:

```scala mdoc
import cats.data.NonEmptyList

NonEmptyList.of(1,2) <+> NonEmptyList.of(3,4)
```

The import adds the `|+|` syntax.

**Note**: Beware that if you import a type class instance or its syntax twice, you will receive conflicting implicits with a less than helpful error message.
This usually happens when importing different type classes in the same hierarchy or when importing syntax enrichment for all type classes using `cats.syntax.all._` or `cats.implicits._` together with a more specific import like `cats.syntax.option._` or `cats.instances.either._`.
Below is an example of this phenomenon:

```scala mdoc:silent:fail
import cats.instances.all._
val x = -2 |+| 1
```scala mdoc:reset:silent:fail
import cats.syntax.all._

1 |+| 2

import cats.syntax.semigroup._

// now we also need access to isEmpty from Monoid
import cats.syntax.monoid._
(x |+| 1).isEmpty //error: value |+| is not a member of Int
3 |+| 5 // error: value |+| is not a member of Int
```

Compilation fails on the second invocation of `|+|` because we now have conflicting implicits from `Monoid` and `Semigroup`.
Compilation fails on the second invocation of `|+|` because we now have conflicting implicits from `Semigroup`.