Class/Object

monix.eval.Coeval

Eager

Related Docs: object Eager | package Coeval

Permalink

sealed abstract class Eager[+A] extends Coeval[A] with Product

The Eager type represents a strict, already evaluated result of a Coeval that either resulted in success, wrapped in a Now, or in an error, wrapped in an Error.

It's the moral equivalent of scala.util.Try, except that application of functions such as map and flatMap produces Coeval references that are still lazily evaluated.

Self Type
Eager[A]
Linear Supertypes
Product, Equals, Coeval[A], Serializable, Serializable, () ⇒ A, AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Eager
  2. Product
  3. Equals
  4. Coeval
  5. Serializable
  6. Serializable
  7. Function0
  8. AnyRef
  9. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def canEqual(that: Any): Boolean

    Permalink
    Definition Classes
    Equals
  2. abstract def productArity: Int

    Permalink
    Definition Classes
    Product
  3. abstract def productElement(n: Int): Any

    Permalink
    Definition Classes
    Product

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def apply(): A

    Permalink

    Evaluates the underlying computation and returns the result.

    Evaluates the underlying computation and returns the result.

    NOTE: this can throw exceptions.

    // For didactic purposes, don't do shared vars at home :-)
    var i = 0
    val fa = Coeval { i += 1; i }
    
    fa() //=> 1
    fa() //=> 2
    fa() //=> 3

    UNSAFE — this operation can trigger the execution of side effects, which break referential transparency and is thus not a pure function.

    In FP code use with care, suspended in another Coeval or Task, or at the edge of the FP program.

    Definition Classes
    Coeval → Function0
  5. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  6. final def attempt: Coeval[Either[Throwable, A]]

    Permalink

    Creates a new Coeval that will expose any triggered error from the source.

    Creates a new Coeval that will expose any triggered error from the source.

    val fa: Coeval[Int] =
      Coeval.raiseError[Int](new DummyException("dummy"))
    
    val fe: Coeval[Either[Throwable, Int]] =
      fa.attempt
    
    fe.flatMap {
      case Left(_) => Int.MaxValue
      case Right(v) => v
    }

    By exposing errors by lifting the Coeval's result into an Either value, we can handle those errors in flatMap transformations.

    Also see materialize for working with Scala's Try or transformWith for an alternative.

    Definition Classes
    Coeval
  7. final def bracket[B](use: (A) ⇒ Coeval[B])(release: (A) ⇒ Coeval[Unit]): Coeval[B]

    Permalink

    Returns a task that treats the source as the acquisition of a resource, which is then exploited by the use function and then released.

    Returns a task that treats the source as the acquisition of a resource, which is then exploited by the use function and then released.

    The bracket operation is the equivalent of the try {} finally {} statements from mainstream languages, installing the necessary exception handler to release the resource in the event of an exception being raised during the computation. If an exception is raised, then bracket will re-raise the exception after performing the release.

    Example:

    import java.io._
    
    def readFile(file: File): Coeval[String] = {
      // Opening a file handle for reading text
      val acquire = Coeval.eval(new BufferedReader(
        new InputStreamReader(new FileInputStream(file), "utf-8")
      ))
    
      acquire.bracket { in =>
        // Usage part
        Coeval.eval {
          // Yes, ugly Java, non-FP loop;
          // side-effects are suspended though
          var line: String = null
          val buff = new StringBuilder()
          do {
            line = in.readLine()
            if (line != null) buff.append(line)
          } while (line != null)
          buff.toString()
        }
      } { in =>
        // The release part
        Coeval.eval(in.close())
      }
    }

    NOTE on error handling: one big difference versus try {} finally {} is that, in case both the release function and the use function throws, the error raised by use gets signaled and the error raised by release gets reported with System.err for Coeval or with Scheduler.reportFailure for Task.

    For example:

    Coeval("resource").bracket { _ =>
      // use
      Coeval.raiseError(new RuntimeException("Foo"))
    } { _ =>
      // release
      Coeval.raiseError(new RuntimeException("Bar"))
    }

    In this case the error signaled downstream is "Foo", while the "Bar" error gets reported. This is consistent with the behavior of Haskell's bracket operation and NOT with try {} finally {} from Scala, Java or JavaScript.

    use

    is a function that evaluates the resource yielded by the source, yielding a result that will get generated by the task returned by this bracket function

    release

    is a function that gets called after use terminates, either normally or in error, receiving as input the resource that needs to be released

    Definition Classes
    Coeval
    See also

    bracketE

  8. final def bracketE[B](use: (A) ⇒ Coeval[B])(release: (A, Either[Throwable, B]) ⇒ Coeval[Unit]): Coeval[B]

    Permalink

    Returns a task that treats the source task as the acquisition of a resource, which is then exploited by the use function and then released, with the possibility of distinguishing between successful termination and error, such that an appropriate release of resources can be executed.

    Returns a task that treats the source task as the acquisition of a resource, which is then exploited by the use function and then released, with the possibility of distinguishing between successful termination and error, such that an appropriate release of resources can be executed.

    The bracket operation is the equivalent of the try {} finally {} statements from mainstream languages, installing the necessary exception handler to release the resource in the event of an exception being raised during the computation. If an exception is raised, then bracket will re-raise the exception after performing the release.

    The release function receives as input:

    • Left(error) in case use terminated with an error
    • Right(b) in case of success

    NOTE on error handling: one big difference versus try {} finally {} is that, in case both the release function and the use function throws, the error raised by use gets signaled and the error raised by release gets reported with System.err for Coeval or with Scheduler.reportFailure for Task.

    For example:

    Coeval("resource").bracket { _ =>
      // use
      Coeval.raiseError(new RuntimeException("Foo"))
    } { _ =>
      // release
      Coeval.raiseError(new RuntimeException("Bar"))
    }

    In this case the error signaled downstream is "Foo", while the "Bar" error gets reported. This is consistent with the behavior of Haskell's bracket operation and NOT with try {} finally {} from Scala, Java or JavaScript.

    use

    is a function that evaluates the resource yielded by the source, yielding a result that will get generated by this function on evaluation

    release

    is a function that gets called after use terminates, either normally or in error, receiving as input the resource that needs that needs release, along with the result of use

    Definition Classes
    Coeval
    See also

    bracket

  9. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  10. final def dematerialize[B](implicit ev: <:<[A, Try[B]]): Coeval[B]

    Permalink

    Dematerializes the source's result from a Try.

    Dematerializes the source's result from a Try.

    This equivalence always holds:

    scala fa.materialize.dematerialize <-> fa

    Definition Classes
    Coeval
  11. final def doOnFinish(f: (Option[Throwable]) ⇒ Coeval[Unit]): Coeval[A]

    Permalink

    Returns a new Coeval in which f is scheduled to be run on completion.

    Returns a new Coeval in which f is scheduled to be run on completion. This would typically be used to release any resources acquired by this Coeval.

    Definition Classes
    Coeval
  12. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  13. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  14. final def failed: Coeval[Throwable]

    Permalink

    Returns a failed projection of this coeval.

    Returns a failed projection of this coeval.

    The failed projection is a Coeval holding a value of type Throwable, emitting the error yielded by the source, in case the source fails, otherwise if the source succeeds the result will fail with a NoSuchElementException.

    Definition Classes
    Coeval
  15. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  16. final def flatMap[B](f: (A) ⇒ Coeval[B]): Coeval[B]

    Permalink

    Creates a new Coeval by applying a function to the successful result of the source, and returns a new instance equivalent to the result of the function.

    Creates a new Coeval by applying a function to the successful result of the source, and returns a new instance equivalent to the result of the function.

    The application of flatMap is always lazy and because of the implementation it is memory safe and thus it can be used in recursive loops.

    Sample:

    def randomEven: Coeval[Int] =
      Coeval(Random.nextInt()).flatMap { x =>
        if (x < 0 || x % 2 == 1)
          randomEven // retry
        else
          Coeval.now(x)
      }
    Definition Classes
    Coeval
  17. final def flatten[B](implicit ev: <:<[A, Coeval[B]]): Coeval[B]

    Permalink

    Given a source Coeval that emits another Coeval, this function flattens the result, returning a Coeval equivalent to the emitted Coeval by the source.

    Given a source Coeval that emits another Coeval, this function flattens the result, returning a Coeval equivalent to the emitted Coeval by the source.

    This equivalence with flatMap always holds:

    scala fa.flatten <-> fa.flatMap(x => x)

    Definition Classes
    Coeval
  18. final def foreach(f: (A) ⇒ Unit): Unit

    Permalink

    Triggers the evaluation of the source, executing the given function for the generated element.

    Triggers the evaluation of the source, executing the given function for the generated element.

    The application of this function has strict behavior, as the coeval is immediately executed.

    Definition Classes
    Coeval
  19. final def foreachL(f: (A) ⇒ Unit): Coeval[Unit]

    Permalink

    Returns a new task that upon evaluation will execute the given function for the generated element, transforming the source into a Coeval[Unit].

    Returns a new task that upon evaluation will execute the given function for the generated element, transforming the source into a Coeval[Unit].

    Similar in spirit with normal foreach, but lazy, as obviously nothing gets executed at this point.

    Definition Classes
    Coeval
  20. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  21. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  22. final def isError: Boolean

    Permalink

    Returns true if result is an error.

  23. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  24. final def isSuccess: Boolean

    Permalink

    Returns true if value is a successful one.

  25. final def map[B](f: (A) ⇒ B): Coeval[B]

    Permalink

    Returns a new Coeval that applies the mapping function to the element emitted by the source.

    Returns a new Coeval that applies the mapping function to the element emitted by the source.

    Can be used for specifying a (lazy) transformation to the result of the source.

    This equivalence with flatMap always holds:

    scala fa.map(f) <-> fa.flatMap(x => Coeval.pure(f(x)))

    Definition Classes
    Coeval
  26. final def materialize: Coeval[Try[A]]

    Permalink

    Creates a new Coeval that will expose any triggered error from the source.

    Creates a new Coeval that will expose any triggered error from the source.

    Also see attempt for working with Scala's Either or transformWith for an alternative.

    Definition Classes
    Coeval
  27. final def memoize: Coeval[A]

    Permalink

    Memoizes (caches) the result of the source and reuses it on subsequent invocations of value.

    Memoizes (caches) the result of the source and reuses it on subsequent invocations of value.

    The resulting coeval will be idempotent, meaning that evaluating the resulting coeval multiple times will have the same effect as evaluating it once.

    UNSAFE — this operation allocates a shared, mutable reference, which can break in certain cases referential transparency, even if this operation guarantees idempotency (i.e. referential transparency implies idempotency, but idempotency does not imply referential transparency).

    The allocation of a mutable reference is known to be a side effect, thus breaking referential transparency, even if calling this method does not trigger the evaluation of side effects suspended by the source.

    Use with care. Sometimes it's easier to just keep a shared, memoized reference to some connection, but keep in mind it might be better to pass such a reference around as a parameter.

    Definition Classes
    Coeval
    See also

    memoizeOnSuccess for a version that only caches successful results

  28. final def memoizeOnSuccess: Coeval[A]

    Permalink

    Memoizes (cache) the successful result of the source and reuses it on subsequent invocations of value.

    Memoizes (cache) the successful result of the source and reuses it on subsequent invocations of value. Thrown exceptions are not cached.

    The resulting coeval will be idempotent, but only if the result is successful.

    UNSAFE — this operation allocates a shared, mutable reference, which can break in certain cases referential transparency, even if this operation guarantees idempotency (i.e. referential transparency implies idempotency, but idempotency does not imply referential transparency).

    The allocation of a mutable reference is known to be a side effect, thus breaking referential transparency, even if calling this method does not trigger the evaluation of side effects suspended by the source.

    Use with care. Sometimes it's easier to just keep a shared, memoized reference to some connection, but keep in mind it might be better to pass such a reference around as a parameter.

    Definition Classes
    Coeval
    See also

    memoize for a version that caches both successful results and failures

  29. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  30. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  31. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  32. final def onErrorFallbackTo[B >: A](that: Coeval[B]): Coeval[B]

    Permalink

    Creates a new coeval that in case of error will fallback to the given backup coeval.

    Creates a new coeval that in case of error will fallback to the given backup coeval.

    Definition Classes
    Coeval
  33. final def onErrorHandle[U >: A](f: (Throwable) ⇒ U): Coeval[U]

    Permalink

    Creates a new coeval that will handle any matching throwable that this coeval might emit.

    Creates a new coeval that will handle any matching throwable that this coeval might emit.

    See onErrorRecover for the version that takes a partial function.

    Definition Classes
    Coeval
  34. final def onErrorHandleWith[B >: A](f: (Throwable) ⇒ Coeval[B]): Coeval[B]

    Permalink

    Creates a new coeval that will handle any matching throwable that this coeval might emit by executing another coeval.

    Creates a new coeval that will handle any matching throwable that this coeval might emit by executing another coeval.

    See onErrorRecoverWith for the version that takes a partial function.

    Definition Classes
    Coeval
  35. final def onErrorRecover[U >: A](pf: PartialFunction[Throwable, U]): Coeval[U]

    Permalink

    Creates a new coeval that on error will try to map the error to another value using the provided partial function.

    Creates a new coeval that on error will try to map the error to another value using the provided partial function.

    See onErrorHandle for the version that takes a total function.

    Definition Classes
    Coeval
  36. final def onErrorRecoverWith[B >: A](pf: PartialFunction[Throwable, Coeval[B]]): Coeval[B]

    Permalink

    Creates a new coeval that will try recovering from an error by matching it with another coeval using the given partial function.

    Creates a new coeval that will try recovering from an error by matching it with another coeval using the given partial function.

    See onErrorHandleWith for the version that takes a total function.

    Definition Classes
    Coeval
  37. final def onErrorRestart(maxRetries: Long): Coeval[A]

    Permalink

    Creates a new coeval that in case of error will retry executing the source again and again, until it succeeds.

    Creates a new coeval that in case of error will retry executing the source again and again, until it succeeds.

    In case of continuous failure the total number of executions will be maxRetries + 1.

    Definition Classes
    Coeval
  38. final def onErrorRestartIf(p: (Throwable) ⇒ Boolean): Coeval[A]

    Permalink

    Creates a new coeval that in case of error will retry executing the source again and again, until it succeeds.

    Creates a new coeval that in case of error will retry executing the source again and again, until it succeeds.

    In case of continuous failure the total number of executions will be maxRetries + 1.

    Definition Classes
    Coeval
  39. final def onErrorRestartLoop[S, B >: A](initial: S)(f: (Throwable, S, (S) ⇒ Coeval[B]) ⇒ Coeval[B]): Coeval[B]

    Permalink

    On error restarts the source with a customizable restart loop.

    On error restarts the source with a customizable restart loop.

    This operation keeps an internal state, with a start value, an internal state that gets evolved and based on which the next step gets decided, e.g. should it restart, or should it give up and rethrow the current error.

    Example that implements a simple retry policy that retries for a maximum of 10 times before giving up:

    import scala.concurrent.duration._
    
    task.onErrorRestartLoop(10) { (err, maxRetries, retry) =>
      if (maxRetries > 0)
        // Do next retry please
        retry(maxRetries - 1)
      else
        // No retries left, rethrow the error
        Task.raiseError(err)
    }

    The given function injects the following parameters:

    1. error reference that was thrown 2. the current state, based on which a decision for the retry is made 3. retry: S => Task[B] function that schedules the next retry
    initial

    is the initial state used to determine the next on error retry cycle

    f

    is a function that injects the current error, state, a function that can signal a retry is to be made and returns the next coeval

    Definition Classes
    Coeval
  40. def productIterator: Iterator[Any]

    Permalink
    Definition Classes
    Product
  41. def productPrefix: String

    Permalink
    Definition Classes
    Product
  42. final def restartUntil(p: (A) ⇒ Boolean): Coeval[A]

    Permalink

    Given a predicate function, keep retrying the coeval until the function returns true.

    Given a predicate function, keep retrying the coeval until the function returns true.

    Definition Classes
    Coeval
  43. def run: Eager[A]

    Permalink

    Evaluates the underlying computation, reducing this Coeval to a Coeval.Eager value, with successful results being signaled with Coeval.Now and failures with Coeval.Error.

    Evaluates the underlying computation, reducing this Coeval to a Coeval.Eager value, with successful results being signaled with Coeval.Now and failures with Coeval.Error.

    val fa = Coeval.eval(10 * 2)
    
    fa.run match {
      case Coeval.Now(value) =>
        println("Success: " + value)
      case Coeval.Error(e) =>
        e.printStackTrace()
    }

    See runAttempt for working with Either values and runTry for working with Try values. See apply for a partial function (that may throw exceptions in case of failure).

    UNSAFE — this operation can trigger the execution of side effects, which break referential transparency and is thus not a pure function.

    In FP code use with care, suspended in another Coeval or Task, or at the edge of the FP program.

    Definition Classes
    Coeval
  44. def runAttempt: Either[Throwable, A]

    Permalink

    Evaluates the underlying computation and returns the result or any triggered errors as a Scala Either, where Right(_) is for successful values and Left(_) is for thrown errors.

    Evaluates the underlying computation and returns the result or any triggered errors as a Scala Either, where Right(_) is for successful values and Left(_) is for thrown errors.

    val fa = Coeval(10 * 2)
    
    fa.runAttempt match {
      case Right(value) =>
        println("Success: " + value)
      case Left(e) =>
        e.printStackTrace()
    }

    See run for working with Coeval.Eager values and runTry for working with Try values. See apply for a partial function (that may throw exceptions in case of failure).

    UNSAFE — this operation can trigger the execution of side effects, which break referential transparency and is thus not a pure function.

    In FP code use with care, suspended in another Coeval or Task, or at the edge of the FP program.

    Definition Classes
    Coeval
  45. def runTry: Try[A]

    Permalink

    Evaluates the underlying computation and returns the result or any triggered errors as a scala.util.Try.

    Evaluates the underlying computation and returns the result or any triggered errors as a scala.util.Try.

    val fa = Coeval(10 * 2)
    
    fa.runTry match {
      case Success(value) =>
        println("Success: " + value)
      case Failure(e) =>
        e.printStackTrace()
    }

    See run for working with Coeval.Eager values and runAttempt for working with Either values. See apply for a partial function (that may throw exceptions in case of failure).

    UNSAFE — this operation can trigger the execution of side effects, which break referential transparency and is thus not a pure function.

    In FP code use with care, suspended in another Coeval or Task, or at the edge of the FP program.

    Definition Classes
    Coeval
  46. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  47. final def task: Task[A]

    Permalink

    Converts the source Coeval into a Task.

    Converts the source Coeval into a Task.

    Definition Classes
    Coeval
  48. final def toEither: Either[Throwable, A]

    Permalink

    Converts this Eager value into a scala.Either.

  49. final def toEval: Eval[A]

    Permalink

    Converts the source Coeval into a cats.Eval.

    Converts the source Coeval into a cats.Eval.

    Definition Classes
    Coeval
  50. final def toIO: IO[A]

    Permalink

    Converts the source Coeval into a cats.effect.IO.

    Converts the source Coeval into a cats.effect.IO.

    Definition Classes
    Coeval
  51. def toString(): String

    Permalink
    Definition Classes
    Coeval → Function0 → AnyRef → Any
  52. final def toTry: Try[A]

    Permalink

    Converts this Eager value into a scala.util.Try.

  53. final def transform[R](fa: (A) ⇒ R, fe: (Throwable) ⇒ R): Coeval[R]

    Permalink

    Creates a new Coeval by applying the 'fa' function to the successful result of this future, or the 'fe' function to the potential errors that might happen.

    Creates a new Coeval by applying the 'fa' function to the successful result of this future, or the 'fe' function to the potential errors that might happen.

    This function is similar with map, except that it can also transform errors and not just successful results.

    For example attempt can be expressed in terms of transform:

    source.transform(v => Right(v), e => Left(e))

    And materialize too:

    source.transform(v => Success(v), e => Failure(e))
    fa

    function that transforms a successful result of the receiver

    fe

    function that transforms an error of the receiver

    Definition Classes
    Coeval
  54. final def transformWith[R](fa: (A) ⇒ Coeval[R], fe: (Throwable) ⇒ Coeval[R]): Coeval[R]

    Permalink

    Creates a new Coeval by applying the 'fa' function to the successful result of this future, or the 'fe' function to the potential errors that might happen.

    Creates a new Coeval by applying the 'fa' function to the successful result of this future, or the 'fe' function to the potential errors that might happen.

    This function is similar with flatMap, except that it can also transform errors and not just successful results.

    For example attempt can be expressed in terms of transformWith:

    source.transformWith(
      v => Coeval.now(Right(v)),
      e => Coeval.now(Left(e))
    )
    fa

    function that transforms a successful result of the receiver

    fe

    function that transforms an error of the receiver

    Definition Classes
    Coeval
  55. def value: A

    Permalink

    Evaluates the underlying computation and returns the result.

    Evaluates the underlying computation and returns the result.

    NOTE: this can throw exceptions.

    Alias for apply.

    UNSAFE — this operation can trigger the execution of side effects, which break referential transparency and is thus not a pure function.

    In FP code use with care, suspended in another Coeval or Task, or at the edge of the FP program.

    Definition Classes
    Coeval
  56. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  57. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  58. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  59. final def zip[B](that: Coeval[B]): Coeval[(A, B)]

    Permalink

    Zips the values of this and that coeval, and creates a new coeval that will emit the tuple of their results.

    Zips the values of this and that coeval, and creates a new coeval that will emit the tuple of their results.

    Definition Classes
    Coeval
  60. final def zipMap[B, C](that: Coeval[B])(f: (A, B) ⇒ C): Coeval[C]

    Permalink

    Zips the values of this and that and applies the given mapping function on their results.

    Zips the values of this and that and applies the given mapping function on their results.

    Definition Classes
    Coeval

Inherited from Product

Inherited from Equals

Inherited from Coeval[A]

Inherited from Serializable

Inherited from Serializable

Inherited from () ⇒ A

Inherited from AnyRef

Inherited from Any

Ungrouped