Class/Object

zio.stm

ZSTM

Related Docs: object ZSTM | package stm

Permalink

final class ZSTM[-R, +E, +A] extends AnyVal

STM[E, A] represents an effect that can be performed transactionally, resulting in a failure E or a value A.

def transfer(receiver: TRef[Int],
             sender: TRef[Int], much: Int): UIO[Int] =
  STM.atomically {
    for {
      balance <- sender.get
      _       <- STM.check(balance >= much)
      _       <- receiver.update(_ + much)
      _       <- sender.update(_ - much)
      newAmnt <- receiver.get
    } yield newAmnt
  }

  val action: UIO[Int] =
    for {
      t <- STM.atomically(TRef.make(0).zip(TRef.make(20000)))
      (receiver, sender) = t
      balance <- transfer(receiver, sender, 1000)
    } yield balance

Software Transactional Memory is a technique which allows composition of arbitrary atomic operations. It is the software analog of transactions in database systems.

The API is lifted directly from the Haskell package Control.Concurrent.STM although the implementation does not resemble the Haskell one at all. http://hackage.haskell.org/package/stm-2.5.0.0/docs/Control-Concurrent-STM.html

STM in Haskell was introduced in: Composable memory transactions, by Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy, in ACM Conference on Principles and Practice of Parallel Programming 2005. https://www.microsoft.com/en-us/research/publication/composable-memory-transactions/

See also: Lock Free Data Structures using STMs in Haskell, by Anthony Discolo, Tim Harris, Simon Marlow, Simon Peyton Jones, Satnam Singh) FLOPS 2006: Eighth International Symposium on Functional and Logic Programming, Fuji Susono, JAPAN, April 2006 https://www.microsoft.com/en-us/research/publication/lock-free-data-structures-using-stms-in-haskell/

Self Type
ZSTM[R, E, A]
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ZSTM
  2. AnyVal
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

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

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

    Permalink
    Definition Classes
    Any
  3. def &&&[R1 <: R, E1 >: E, B](that: ZSTM[R1, E1, B]): ZSTM[R1, E1, (A, B)]

    Permalink

    Alias for <*> and zip.

  4. def ***[R1, E1 >: E, B](that: ZSTM[R1, E1, B]): ZSTM[(R, R1), E1, (A, B)]

    Permalink

    Splits the environment, providing the first part to this effect and the second part to that effect.

  5. def *>[R1 <: R, E1 >: E, B](that: ⇒ ZSTM[R1, E1, B]): ZSTM[R1, E1, B]

    Permalink

    Sequentially zips this value with the specified one, discarding the first element of the tuple.

  6. def +++[R1, B, E1 >: E](that: ZSTM[R1, E1, B]): ZSTM[Either[R, R1], E1, Either[A, B]]

    Permalink

    Depending on provided environment, returns either this one or the other effect lifted in Left or Right, respectively.

  7. def <*[R1 <: R, E1 >: E, B](that: ⇒ ZSTM[R1, E1, B]): ZSTM[R1, E1, A]

    Permalink

    Sequentially zips this value with the specified one, discarding the second element of the tuple.

  8. def <*>[R1 <: R, E1 >: E, B](that: ⇒ ZSTM[R1, E1, B]): ZSTM[R1, E1, (A, B)]

    Permalink

    Sequentially zips this value with the specified one.

  9. def <<<[R1, E1 >: E](that: ZSTM[R1, E1, R]): ZSTM[R1, E1, A]

    Permalink

    Propagates self environment to that.

  10. def <>[R1 <: R, E1, A1 >: A](that: ⇒ ZSTM[R1, E1, A1])(implicit ev: CanFail[E]): ZSTM[R1, E1, A1]

    Permalink

    Tries this effect first, and if it fails, tries the other effect.

  11. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  12. def >>=[R1 <: R, E1 >: E, B](f: (A) ⇒ ZSTM[R1, E1, B]): ZSTM[R1, E1, B]

    Permalink

    Feeds the value produced by this effect to the specified function, and then runs the returned effect as well to produce its results.

  13. def >>>[R1 >: A, E1 >: E, B](that: ZSTM[R1, E1, B]): ZSTM[R, E1, B]

    Permalink

    Propagates the given environment to self.

  14. def absolve[R1 <: R, E1, B](implicit ev1: <:<[ZSTM[R, E, A], ZSTM[R1, E1, Either[E1, B]]]): ZSTM[R1, E1, B]

    Permalink

    Returns an effect that submerges the error case of an Either into the STM.

    Returns an effect that submerges the error case of an Either into the STM. The inverse operation of STM.either.

  15. def andThen[R1 >: A, E1 >: E, B](that: ZSTM[R1, E1, B]): ZSTM[R, E1, B]

    Permalink

    Named alias for >>>.

  16. def as[B](b: ⇒ B): ZSTM[R, E, B]

    Permalink

    Maps the success value of this effect to the specified constant value.

  17. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  18. def asSome: ZSTM[R, E, Option[A]]

    Permalink

    Maps the success value of this effect to an optional value.

  19. def asSomeError: ZSTM[R, Option[E], A]

    Permalink

    Maps the error value of this effect to an optional value.

  20. def bimap[E2, B](f: (E) ⇒ E2, g: (A) ⇒ B)(implicit ev: CanFail[E]): ZSTM[R, E2, B]

    Permalink

    Returns an STM effect whose failure and success channels have been mapped by the specified pair of functions, f and g.

  21. def catchAll[R1 <: R, E2, A1 >: A](h: (E) ⇒ ZSTM[R1, E2, A1])(implicit ev: CanFail[E]): ZSTM[R1, E2, A1]

    Permalink

    Recovers from all errors.

  22. def catchSome[R1 <: R, E1 >: E, A1 >: A](pf: PartialFunction[E, ZSTM[R1, E1, A1]])(implicit ev: CanFail[E]): ZSTM[R1, E1, A1]

    Permalink

    Recovers from some or all of the error cases.

  23. def collect[B](pf: PartialFunction[A, B]): ZSTM[R, E, B]

    Permalink

    Simultaneously filters and maps the value produced by this effect.

  24. def collectM[R1 <: R, E1 >: E, B](pf: PartialFunction[A, ZSTM[R1, E1, B]]): ZSTM[R1, E1, B]

    Permalink

    Simultaneously filters and flatMaps the value produced by this effect.

    Simultaneously filters and flatMaps the value produced by this effect. Continues on the effect returned from pf.

  25. def commit: ZIO[R, E, A]

    Permalink

    Commits this transaction atomically.

  26. def commitEither: ZIO[R, E, A]

    Permalink

    Commits this transaction atomically, regardless of whether the transaction is a success or a failure.

  27. def compose[R1, E1 >: E](that: ZSTM[R1, E1, R]): ZSTM[R1, E1, A]

    Permalink

    Named alias for <<<.

  28. def doUntil(f: (A) ⇒ Boolean): ZSTM[R, E, A]

    Permalink

    Repeats this STM effect until its result satisfies the specified predicate.

  29. def doWhile(f: (A) ⇒ Boolean): ZSTM[R, E, A]

    Permalink

    Repeats this STM effect while its result satisfies the specified predicate.

  30. def either(implicit ev: CanFail[E]): ZSTM[R, Nothing, Either[E, A]]

    Permalink

    Converts the failure channel into an Either.

  31. def ensuring[R1 <: R](finalizer: ZSTM[R1, Nothing, Any]): ZSTM[R1, E, A]

    Permalink

    Executes the specified finalization transaction whether or not this effect succeeds.

    Executes the specified finalization transaction whether or not this effect succeeds. Note that as with all STM transactions, if the full transaction fails, everything will be rolled back.

  32. def eventually(implicit ev: CanFail[E]): ZSTM[R, Nothing, A]

    Permalink

    Returns an effect that ignores errors and runs repeatedly until it eventually succeeds.

  33. def filterOrDie(p: (A) ⇒ Boolean)(t: ⇒ Throwable): ZSTM[R, E, A]

    Permalink

    Dies with specified Throwable if the predicate fails.

  34. def filterOrDieMessage(p: (A) ⇒ Boolean)(msg: ⇒ String): ZSTM[R, E, A]

    Permalink

    Dies with a java.lang.RuntimeException having the specified text message if the predicate fails.

  35. def filterOrElse[R1 <: R, E1 >: E, A1 >: A](p: (A) ⇒ Boolean)(f: (A) ⇒ ZSTM[R1, E1, A1]): ZSTM[R1, E1, A1]

    Permalink

    Applies f if the predicate fails.

  36. def filterOrElse_[R1 <: R, E1 >: E, A1 >: A](p: (A) ⇒ Boolean)(zstm: ⇒ ZSTM[R1, E1, A1]): ZSTM[R1, E1, A1]

    Permalink

    Supplies zstm if the predicate fails.

  37. def filterOrFail[E1 >: E](p: (A) ⇒ Boolean)(e: ⇒ E1): ZSTM[R, E1, A]

    Permalink

    Fails with e if the predicate fails.

  38. def flatMap[R1 <: R, E1 >: E, B](f: (A) ⇒ ZSTM[R1, E1, B]): ZSTM[R1, E1, B]

    Permalink

    Feeds the value produced by this effect to the specified function, and then runs the returned effect as well to produce its results.

  39. def flatMapError[R1 <: R, E2](f: (E) ⇒ ZSTM[R1, Nothing, E2])(implicit ev: CanFail[E]): ZSTM[R1, E2, A]

    Permalink

    Creates a composite effect that represents this effect followed by another one that may depend on the error produced by this one.

  40. def flatten[R1 <: R, E1 >: E, B](implicit ev: <:<[A, ZSTM[R1, E1, B]]): ZSTM[R1, E1, B]

    Permalink

    Flattens out a nested STM effect.

  41. def flattenErrorOption[E1, E2 <: E1](default: E2)(implicit ev: <:<[E, Option[E1]]): ZSTM[R, E1, A]

    Permalink

    Unwraps the optional error, defaulting to the provided value.

  42. def flip(implicit ev: CanFail[E]): ZSTM[R, A, E]

    Permalink

    Flips the success and failure channels of this transactional effect.

    Flips the success and failure channels of this transactional effect. This allows you to use all methods on the error channel, possibly before flipping back.

  43. def flipWith[R1, A1, E1](f: (ZSTM[R, A, E]) ⇒ ZSTM[R1, A1, E1]): ZSTM[R1, E1, A1]

    Permalink

    Swaps the error/value parameters, applies the function f and flips the parameters back

  44. def fold[B](f: (E) ⇒ B, g: (A) ⇒ B)(implicit ev: CanFail[E]): ZSTM[R, Nothing, B]

    Permalink

    Folds over the STM effect, handling both failure and success, but not retry.

  45. def foldM[R1 <: R, E1, B](f: (E) ⇒ ZSTM[R1, E1, B], g: (A) ⇒ ZSTM[R1, E1, B])(implicit ev: CanFail[E]): ZSTM[R1, E1, B]

    Permalink

    Effectfully folds over the STM effect, handling both failure and success.

  46. def forever: ZSTM[R, E, Nothing]

    Permalink

    Repeats this effect forever (until the first error).

  47. def get[E1 >: E, B](implicit ev1: =:=[E1, Nothing], ev2: <:<[A, Option[B]]): ZSTM[R, Unit, B]

    Permalink

    Unwraps the optional success of this effect, but can fail with unit value.

  48. def getClass(): Class[_ <: AnyVal]

    Permalink
    Definition Classes
    AnyVal → Any
  49. def head[B](implicit ev: <:<[A, List[B]]): ZSTM[R, Option[E], B]

    Permalink

    Returns a successful effect with the head of the list if the list is non-empty or fails with the error None if the list is empty.

  50. def ignore: ZSTM[R, Nothing, Unit]

    Permalink

    Returns a new effect that ignores the success or failure of this effect.

  51. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  52. def left[B, C](implicit ev: <:<[A, Either[B, C]]): ZSTM[R, Option[E], B]

    Permalink

    Returns a successful effect if the value is Left, or fails with the error None.

  53. def leftOrFail[B, C, E1 >: E](e: ⇒ E1)(implicit ev: <:<[A, Either[B, C]]): ZSTM[R, E1, B]

    Permalink

    Returns a successful effect if the value is Left, or fails with the error e.

  54. def leftOrFailException[B, C, E1 >: NoSuchElementException](implicit ev: <:<[A, Either[B, C]], ev2: <:<[E, E1]): ZSTM[R, E1, B]

    Permalink

    Returns a successful effect if the value is Left, or fails with a java.util.NoSuchElementException.

  55. def map[B](f: (A) ⇒ B): ZSTM[R, E, B]

    Permalink

    Maps the value produced by the effect.

  56. def mapError[E1](f: (E) ⇒ E1)(implicit ev: CanFail[E]): ZSTM[R, E1, A]

    Permalink

    Maps from one error type to another.

  57. def merge[A1 >: A](implicit ev1: <:<[E, A1], ev2: CanFail[E]): ZSTM[R, Nothing, A1]

    Permalink

    Returns a new effect where the error channel has been merged into the success channel to their common combined type.

  58. def none[B](implicit ev: <:<[A, Option[B]]): ZSTM[R, Option[E], Unit]

    Permalink

    Requires the option produced by this value to be None.

  59. def onFirst[R1 <: R]: ZSTM[R1, E, (A, R1)]

    Permalink

    Propagates the success value to the first element of a tuple, but passes the effect input R along unmodified as the second element of the tuple.

  60. def onLeft[C]: ZSTM[Either[R, C], E, Either[A, C]]

    Permalink

    Returns this effect if environment is on the left, otherwise returns whatever is on the right unmodified.

    Returns this effect if environment is on the left, otherwise returns whatever is on the right unmodified. Note that the result is lifted in either.

  61. def onRight[C]: ZSTM[Either[C, R], E, Either[C, A]]

    Permalink

    Returns this effect if environment is on the right, otherwise returns whatever is on the left unmodified.

    Returns this effect if environment is on the right, otherwise returns whatever is on the left unmodified. Note that the result is lifted in either.

  62. def onSecond[R1 <: R]: ZSTM[R1, E, (R1, A)]

    Permalink

    Propagates the success value to the second element of a tuple, but passes the effect input R along unmodified as the first element of the tuple.

  63. def option(implicit ev: CanFail[E]): ZSTM[R, Nothing, Option[A]]

    Permalink

    Converts the failure channel into an Option.

  64. def optional[E1](implicit ev: <:<[E, Option[E1]]): ZSTM[R, E1, Option[A]]

    Permalink

    Converts an option on errors into an option on values.

  65. def orDie[E1 >: E](implicit ev1: <:<[E1, Throwable], ev2: CanFail[E]): ZSTM[R, Nothing, A]

    Permalink

    Translates STM effect failure into death of the fiber, making all failures unchecked and not a part of the type of the effect.

  66. def orDieWith(f: (E) ⇒ Throwable)(implicit ev: CanFail[E]): ZSTM[R, Nothing, A]

    Permalink

    Keeps none of the errors, and terminates the fiber running the STM effect with them, using the specified function to convert the E into a Throwable.

  67. def orElse[R1 <: R, E1, A1 >: A](that: ⇒ ZSTM[R1, E1, A1])(implicit ev: CanFail[E]): ZSTM[R1, E1, A1]

    Permalink

    Named alias for <>.

  68. def orElseEither[R1 <: R, E1 >: E, B](that: ⇒ ZSTM[R1, E1, B])(implicit ev: CanFail[E]): ZSTM[R1, E1, Either[A, B]]

    Permalink

    Returns a transactional effect that will produce the value of this effect in left side, unless it fails, in which case, it will produce the value of the specified effect in right side.

  69. def orElseFail[E1](e1: ⇒ E1)(implicit ev: CanFail[E]): ZSTM[R, E1, A]

    Permalink

    Tries this effect first, and if it fails, fails with the specified error.

  70. def orElseSucceed[A1 >: A](a1: ⇒ A1)(implicit ev: CanFail[E]): ZSTM[R, Nothing, A1]

    Permalink

    Tries this effect first, and if it fails, succeeds with the specified value.

  71. def provide(r: R): ZSTM[Any, E, A]

    Permalink

    Provides the transaction its required environment, which eliminates its dependency on R.

  72. def provideSome[R0](f: (R0) ⇒ R): ZSTM[R0, E, A]

    Permalink

    Provides some of the environment required to run this effect, leaving the remainder R0.

  73. def refineOrDie[E1](pf: PartialFunction[E, E1])(implicit ev1: <:<[E, Throwable], ev2: CanFail[E]): ZSTM[R, E1, A]

    Permalink

    Keeps some of the errors, and terminates the fiber with the rest.

  74. def refineOrDieWith[E1](pf: PartialFunction[E, E1])(f: (E) ⇒ Throwable)(implicit ev: CanFail[E]): ZSTM[R, E1, A]

    Permalink

    Keeps some of the errors, and terminates the fiber with the rest, using the specified function to convert the E into a Throwable.

  75. def reject[R1 <: R, E1 >: E](pf: PartialFunction[A, E1]): ZSTM[R1, E1, A]

    Permalink

    Fail with the returned value if the PartialFunction matches, otherwise continue with our held value.

  76. def rejectM[R1 <: R, E1 >: E](pf: PartialFunction[A, ZSTM[R1, E1, E1]]): ZSTM[R1, E1, A]

    Permalink

    Continue with the returned computation if the PartialFunction matches, translating the successful match into a failure, otherwise continue with our held value.

  77. def retryUntil(f: (A) ⇒ Boolean): ZSTM[R, E, A]

    Permalink

    Filters the value produced by this effect, retrying the transaction until the predicate returns true for the value.

  78. def retryWhile(f: (A) ⇒ Boolean): ZSTM[R, E, A]

    Permalink

    Filters the value produced by this effect, retrying the transaction while the predicate returns true for the value.

  79. def right[B, C](implicit ev: <:<[A, Either[B, C]]): ZSTM[R, Option[E], C]

    Permalink

    Returns a successful effect if the value is Right, or fails with the error None.

  80. def rightOrFail[B, C, E1 >: E](e: ⇒ E1)(implicit ev: <:<[A, Either[B, C]]): ZSTM[R, E1, C]

    Permalink

    Returns a successful effect if the value is Right, or fails with the given error 'e'.

  81. def rightOrFailException[B, C, E1 >: NoSuchElementException](implicit ev: <:<[A, Either[B, C]], ev2: <:<[E, E1]): ZSTM[R, E1, C]

    Permalink

    Returns a successful effect if the value is Right, or fails with a java.util.NoSuchElementException.

  82. def some[B](implicit ev: <:<[A, Option[B]]): ZSTM[R, Option[E], B]

    Permalink

    Converts an option on values into an option on errors.

  83. def someOrFail[B, E1 >: E](e: ⇒ E1)(implicit ev: <:<[A, Option[B]]): ZSTM[R, E1, B]

    Permalink

    Extracts the optional value, or fails with the given error 'e'.

  84. def someOrFailException[B, E1 >: E](implicit ev: <:<[A, Option[B]], ev2: <:<[NoSuchElementException, E1]): ZSTM[R, E1, B]

    Permalink

    Extracts the optional value, or fails with a java.util.NoSuchElementException

  85. def summarized[R1 <: R, E1 >: E, B, C](summary: ZSTM[R1, E1, B])(f: (B, B) ⇒ C): ZSTM[R1, E1, (C, A)]

    Permalink

    Summarizes a STM effect by computing a provided value before and after execution, and then combining the values to produce a summary, together with the result of execution.

  86. def tap[R1 <: R, E1 >: E](f: (A) ⇒ ZSTM[R1, E1, Any]): ZSTM[R1, E1, A]

    Permalink

    "Peeks" at the success of transactional effect.

  87. def tapBoth[R1 <: R, E1 >: E](f: (E) ⇒ ZSTM[R1, E1, Any], g: (A) ⇒ ZSTM[R1, E1, Any])(implicit ev: CanFail[E]): ZSTM[R1, E1, A]

    Permalink

    "Peeks" at both sides of an transactional effect.

  88. def tapError[R1 <: R, E1 >: E](f: (E) ⇒ ZSTM[R1, E1, Any])(implicit ev: CanFail[E]): ZSTM[R1, E1, A]

    Permalink

    "Peeks" at the error of the transactional effect.

  89. def toString(): String

    Permalink
    Definition Classes
    Any
  90. def unit: ZSTM[R, E, Unit]

    Permalink

    Maps the success value of this effect to unit.

  91. def when(b: Boolean): ZSTM[R, E, Unit]

    Permalink

    The moral equivalent of if (p) exp

  92. def whenM[R1 <: R, E1 >: E](b: ZSTM[R1, E1, Boolean]): ZSTM[R1, E1, Unit]

    Permalink

    The moral equivalent of if (p) exp when p has side-effects

  93. def withFilter(f: (A) ⇒ Boolean): ZSTM[R, E, A]

    Permalink

    Same as retryUntil.

  94. def zip[R1 <: R, E1 >: E, B](that: ⇒ ZSTM[R1, E1, B]): ZSTM[R1, E1, (A, B)]

    Permalink

    Named alias for <*>.

  95. def zipLeft[R1 <: R, E1 >: E, B](that: ⇒ ZSTM[R1, E1, B]): ZSTM[R1, E1, A]

    Permalink

    Named alias for <*.

  96. def zipRight[R1 <: R, E1 >: E, B](that: ⇒ ZSTM[R1, E1, B]): ZSTM[R1, E1, B]

    Permalink

    Named alias for *>.

  97. def zipWith[R1 <: R, E1 >: E, B, C](that: ⇒ ZSTM[R1, E1, B])(f: (A, B) ⇒ C): ZSTM[R1, E1, C]

    Permalink

    Sequentially zips this value with the specified one, combining the values using the specified combiner function.

  98. def |||[R1, E1 >: E, A1 >: A](that: ZSTM[R1, E1, A1]): ZSTM[Either[R, R1], E1, A1]

    Permalink

    Depending on provided environment returns either this one or the other effect.

Deprecated Value Members

  1. def asError[E1](e: ⇒ E1)(implicit ev: CanFail[E]): ZSTM[R, E1, A]

    Permalink

    Maps the error value of this effect to the specified constant value.

    Maps the error value of this effect to the specified constant value.

    Annotations
    @deprecated
    Deprecated

    (Since version 1.0.0) use orElseFail

  2. def fallback[A1 >: A](a: ⇒ A1)(implicit ev: CanFail[E]): ZSTM[R, Nothing, A1]

    Permalink

    Tries this effect first, and if it fails, succeeds with the specified value.

    Tries this effect first, and if it fails, succeeds with the specified value.

    Annotations
    @deprecated
    Deprecated

    (Since version 1.0.0) use orElseSucceed

Inherited from AnyVal

Inherited from Any

Ungrouped