arrow-fx-stm / arrow.fx.stm / STM / get

get

open operator fun <A> TArray<A>.get(i: Int): A

Read a variable from the TArray.

import arrow.fx.stm.TArray
import arrow.fx.stm.atomically

suspend fun main() {
  //sampleStart
  val tarr = TArray.new(size = 10, 2)
  val result = atomically {
    tarr[5]
  }
  //sampleEnd
  println("Result $result")
}

Throws if i is out of bounds.

This function never retries.

open operator fun <K, V> TMap<K, V>.get(k: K): V?

Alias of STM.lookup

import arrow.fx.stm.TMap
import arrow.fx.stm.atomically

suspend fun main() {
  //sampleStart
  val tmap = TMap.new<Int, String>()
  val result = atomically {
    tmap[1] = "Hello"
    tmap[2] = "World"

    tmap[2]
  }
  //sampleEnd
  println("Result $result")
}

If the key is not present STM.get will not retry, instead it returns null.

Do you like Arrow?

Arrow Org
<