sidnt / proden

prog ram de sign n otes
0 stars 0 forks source link

Understanding Ref Semantics #5

Open sidnt opened 4 years ago

sidnt commented 4 years ago

Why? UseCase: Can we use zio to setState of a component?

Ref Doc on zioµsite Ref scaladoc
. . What is a volatile var a:A volatile means it is directly read-from/written-to the main memory, no cpu caching happens. var means, the value a on the other end of the var, can be mutated in place to become a different A, by whoever can access the var. The var is still immutable at the typelevel, ie, its type can't be changed. It's value can be changed, but it will always conform to type A. . .

A mutable atomic reference for the IO monad. This is the IO equivalent of a volatile var, augmented with atomic operations, which make it useful as a reasonably efficient (if low-level) concurrency primitive.

Ref[A] models a mutable reference to a value of type A.

Mutable Reference Semantics: I have an address in my hand. The address can change. But it's guaranteed that, the reference will always lead to an a:A

Construction: final def make[A](a: A): UIO[Ref[A]] Can be done via Ref.make(someA) where someA: A, flatmapping to an ra: Ref[A] Get: Calling ra.get