Frequently Asked Questions

What imports do I need?

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

import cats._
import cats.data._
import cats.implicits._

The cats._ import brings in quite a few typeclasses (similar to interfaces) such as Monad, Semigroup, and Foldable. Instead of the entire cats package, you can import only the types that you need, for example:

import cats.Monad
import cats.Semigroup
import cats.Foldable

The cats.data._, import brings in data structures such as Xor, Validated, and State. Instead of the entire cats.data package, you can import only the types that you need, for example:

import cats.data.Xor
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, for example:

// cats adds a toXor method to the standard library's Either
val e: Either[String, Int] = Right(3)
// e: Either[String,Int] = Right(3)

e.toXor
// res1: cats.data.Xor[String,Int] = Right(3)

// cats adds an orEmpty method to the standard library's Option
val o: Option[String] = None
// o: Option[String] = None

o.orEmpty
// res3: String = ""

Note: if you import cats.implicits._ (the preferred method), you should not also use imports like cats.syntax.option._ or cats.std.either._. This can result in ambiguous implicit values that cause bewildering compile errors.

Why can't the compiler find implicit instances for Future?

If you have already followed the imports advice but are still getting error messages like could not find implicit value for parameter e: cats.Monad[scala.concurrent.Future] or value |+| is not a member of scala.concurrent.Future[Int], then make sure that you have an implicit scala.concurrent.ExecutionContext in scope. The easiest way to do this is to import scala.concurrent.ExecutionContext.Implicits.global, but note that you may want to use a different execution context for your production application.

How can I turn my List of <something> into a <something> of a list?

It's really common to have a List of values with types like Option, Xor, or Validated that you would like to turn "inside out" into an Option (or Xor or Validated) of a List. The sequence, sequenceU, traverse, and traverseU methods are really handy for this. You can read more about them in the Traverse documentation.

How can I help?

The cats community welcomes and encourages contributions, even if you are completely new to cats and functional programming. Here are a few ways to help out:

  • Find an undocumented method and write a ScalaDoc entry for it. See Arrow.scala for some examples of ScalaDoc entries that use sbt-doctest.
  • Look at the code coverage report, find some untested code, and write a test for it. Even simple helper methods and syntax enrichment should be tested.
  • Find an open issue, leave a comment on it to let people know you are working on it, and submit a pull request. If you are new to cats, you may want to look for items with the low-hanging-fruit label.

See the contributing guide for more information.

results matching ""

    No results matching ""