Raise
Raise[F, E]
expresses the ability to raise errors of type E
in a functorial F[_]
context.
This means that a value of type F[A]
may contain no A
values but instead an E
error value,
and further map
calls will not have any effect due to there being no value to execute the passed function on.
In that sense it is a less powerful version of ApplicativeError
found in cats-core.
In general, however, raiseError
from ApplicativeError
and raise
from Raise
should be equivalent.
The raise
function has the following signature:
trait Raise[F[_], E] {
def raise[A](e: E): F[A]
}
Here's a quick example of how you might use it:
import cats._
import cats.syntax.all._
import cats.mtl._
def parseNumber[F[_]: Applicative](in: String)(implicit F: Raise[F, String]): F[Int] = {
if (in.matches("-?[0-9]+")) in.toInt.pure[F]
else F.raise(show"'$in' could not be parsed as a number")
}
val valid = parseNumber[Either[String, *]]("123")
// valid: Either[String, Int] = Right(value = 123)
val invalid = parseNumber[Either[String, *]]("123abc")
// invalid: Either[String, Int] = Left(
// value = "'123abc' could not be parsed as a number"
// )