-
-
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 piecemeal import guide #1756
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,40 @@ val o: Option[String] = None | |
o.orEmpty | ||
``` | ||
|
||
**Note**: if you import `cats.implicits._` (the preferred method), you should _not_ also use imports like `cats.syntax.option._` or `cats.instances.either._`. This can result in ambiguous implicit values that cause bewildering compile errors. | ||
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: | ||
```tut:book | ||
import cats.instances.string._ | ||
import cats.syntax.monoid._ | ||
|
||
"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 it's `SemigroupK` instance, we would not need to specifically import the instance: | ||
|
||
```tut:book | ||
import cats.data.NonEmptyList | ||
import cats.syntax.semigroupk._ | ||
|
||
NonEmptyList.of(1,2) <+> NonEmptyList.of(3,4) | ||
``` | ||
|
||
|
||
**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: | ||
```tut:silent | ||
import cats.instances.all._ | ||
import cats.syntax.semigroup._ | ||
val x = -2 |+| 1 | ||
|
||
//now we also need access to isEmpty from Monoid | ||
import cats.syntax.monoid._ | ||
(x |+| 1).isEmpty //error: value |+| is not a member of Int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If You would only have this issue when you use |
||
``` | ||
|
||
Compilation fails on the second invocation of `|+|` because we now have conflicting implicits from `Monoid` and `Semigroup`. |
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.
I was surprised this actually worked and we didn't need
import cats.syntax.semigroup._
, but it seems thatMonoidSyntax
extendsSemigroupSyntax
.It seems that all the kernel syntax extends their "super syntax traits", which is another inconsistency between cats-kernel and cats-core (even though this syntax is in cats-core).