Class/Object

zio.stm

STM

Related Docs: object STM | package stm

Permalink

final class STM[+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
STM[E, A]
Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. STM
  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. final def *>[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, B]

    Permalink

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

  4. final def <*[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, A]

    Permalink

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

  5. final def <*>[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, (A, B)]

    Permalink

    Sequentially zips this value with the specified one.

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

    Permalink
    Definition Classes
    Any
  7. final def >>=[E1 >: E, B](f: (A) ⇒ STM[E1, B]): STM[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.

  8. final def as[B](b: ⇒ B): STM[E, B]

    Permalink

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

  9. final def asError[E1](e: ⇒ E1): STM[E1, A]

    Permalink

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

  10. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  11. final def collect[B](pf: PartialFunction[A, B]): STM[E, B]

    Permalink

    Simultaneously filters and maps the value produced by this effect.

  12. final def commit: IO[E, A]

    Permalink

    Commits this transaction atomically.

  13. final def either: STM[Nothing, Either[E, A]]

    Permalink

    Converts the failure channel into an Either.

  14. val exec: (Journal) ⇒ TRez[E, A]

    Permalink
  15. final def filter(f: (A) ⇒ Boolean): STM[E, A]

    Permalink

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

  16. final def flatMap[E1 >: E, B](f: (A) ⇒ STM[E1, B]): STM[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.

  17. final def flatten[E1 >: E, B](implicit ev: <:<[A, STM[E1, B]]): STM[E1, B]

    Permalink

    Flattens out a nested STM effect.

  18. final def fold[B](f: (E) ⇒ B, g: (A) ⇒ B): STM[Nothing, B]

    Permalink

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

  19. final def foldM[E1, B](f: (E) ⇒ STM[E1, B], g: (A) ⇒ STM[E1, B]): STM[E1, B]

    Permalink

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

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

    Permalink
    Definition Classes
    AnyVal → Any
  21. final def ignore: STM[Nothing, Unit]

    Permalink

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

  22. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  23. final def map[B](f: (A) ⇒ B): STM[E, B]

    Permalink

    Maps the value produced by the effect.

  24. final def mapError[E1](f: (E) ⇒ E1): STM[E1, A]

    Permalink

    Maps from one error type to another.

  25. final def option: STM[Nothing, Option[A]]

    Permalink

    Converts the failure channel into an Option.

  26. final def orElse[E1, A1 >: A](that: ⇒ STM[E1, A1]): STM[E1, A1]

    Permalink

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

  27. final def orElseEither[E1 >: E, B](that: ⇒ STM[E1, B]): STM[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.

  28. def toString(): String

    Permalink
    Definition Classes
    Any
  29. final def unit: STM[E, Unit]

    Permalink

    Maps the success value of this effect to unit.

  30. final def withFilter(f: (A) ⇒ Boolean): STM[E, A]

    Permalink

    Same as filter

  31. final def zip[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, (A, B)]

    Permalink

    Named alias for <*>.

  32. final def zipLeft[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, A]

    Permalink

    Named alias for <*.

  33. final def zipRight[E1 >: E, B](that: ⇒ STM[E1, B]): STM[E1, B]

    Permalink

    Named alias for *>.

  34. final def zipWith[E1 >: E, B, C](that: ⇒ STM[E1, B])(f: (A, B) ⇒ C): STM[E1, C]

    Permalink

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

Deprecated Value Members

  1. final def const[B](b: ⇒ B): STM[E, B]

    Permalink
    Annotations
    @deprecated
    Deprecated

    (Since version 1.0.0) use as

  2. final def void: STM[E, Unit]

    Permalink

    Maps the success value of this effect to unit.

    Maps the success value of this effect to unit.

    Annotations
    @deprecated
    Deprecated

    (Since version 1.0.0) use unit

Inherited from AnyVal

Inherited from Any

Ungrouped