I have one class in Scala with four parameters 2 of them is variable and I wanted to use the Ref data type in Zio to control the access to those variable here is my code :
import zio._
class Rectangle(val width: Int,val height: Int) {
val x: UIO[Ref[Int]] = Ref.make(0)
val y: UIO[Ref[Int]] = Ref.make(0)
def this(x1: Int, y1: Int, width: Int, height: Int) {
this(width, height)
for {
ref <- this.x
_ <- ref.set(x1)
} yield ()
for {
ref <- this.y
_ <- ref.set(y1)
} yield ()
}
}
in order to access the Ref I wrote this :
import zio.console._
import zio._
object AccessRef extends App {
val myRec = new Rectangle(1, 2, 3, 4)
override def run(args: List[String]) =
for {
rec <- IO.succeed(myRec)
x <- rec.x
x1 <- x.get
_ <- putStrLn(x1.toString)
_ <-putStrLn(rec.height.toString)
} yield (0)
}
the output:
0
4
I'm wondering why the value of ref couldn't been updated to 1 rather it 0 ?