Reval

jam.monad.Reval
See theReval companion object
sealed abstract class Reval[F[_], +A]

Reval is a data structure which encodes the idea of allocating an object which has an associated finalizer. Can be thought of as a mix of cats.effect.Resource and cats.Eval. There are two allocation strategies:

  • later: allocates the object once when it is needed
  • always: allocates the object every time it is needed

For example:

scala> var c = 0
scala> val a = Reval.thunkLater[IO, Int]{c += 1; c}
scala> a.replicateA(3).map(_.sum).usePure.unsafeRunSync() // List(1, 1, 1).sum
val res0: Int = 3
scala> var c = 0
scala> val a = Reval.thunkAlways[IO, Int]{c += 1; c}
scala> a.replicateA(3).map(_.sum).usePure.unsafeRunSync() // List(1, 2, 3).sum
val res0: Int = 6

Common pitfalls:

  • later with multiple use calls will allocate later objects for each use
scala> var c = 0
scala> val a = Reval.thunkLater[IO, Int] { c += 1; c }
scala> (a.usePure, a.usePure).mapN(_ + _).unsafeRunSync() // 1 + 2
val res1: Int = 3

Attributes

Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class Eval[F, A]
class FlatMap[F, A, B]
class Memoize[F, A]

Members list

Concise view

Value members

Concrete methods

def asResource: Resource[F, A]

Represents a reval as a cats.effect.Resource.

Represents a reval as a cats.effect.Resource.

Attributes

def attempt[E](implicit F: ApplicativeError[F, E]): Reval[F, Either[E, A]]
def combine[B >: A : Semigroup](that: Reval[F, B]): Reval[F, B]
def flatMap[B](f: A => Reval[F, B]): Reval[F, B]
def handleErrorWith[B >: A, E](f: E => Reval[F, B])(implicit E: ApplicativeError[F, E]): Reval[F, B]
def map[B](f: A => B): Reval[F, B]
def memoize[B](implicit ev: A <:< B, B: RevalKeyGen[B]): Reval[F, B]

Ensures that the object will be allocated once.

Ensures that the object will be allocated once.

Attributes

def postFinalize(f: F[Unit])(implicit F: Applicative[F]): Reval[F, A]

Evaluates f after the object finalizer is called.

Evaluates f after the object finalizer is called.

Attributes

def preAllocate(f: F[Unit]): Reval[F, A]

Evaluates f before the object is allocated.

Evaluates f before the object is allocated.

Attributes

def thunkPostFinalize(f: => Unit)(implicit F: Sync[F]): Reval[F, A]
def thunkPreAllocate(f: => Unit)(implicit F: Sync[F]): Reval[F, A]
def use[B](f: A => F[B])(implicit F: MonadCancel[F, Throwable]): F[B]

Allocates an object and supplies it to the given function. The finalizer is called as soon as the resulting F[B] is completed, whether normally or as a raised error.

Allocates an object and supplies it to the given function. The finalizer is called as soon as the resulting F[B] is completed, whether normally or as a raised error.

Attributes

def usePure[B](implicit ev: A <:< B, F: MonadCancel[F, Throwable]): F[B]