Сообщения

Сообщения за ноябрь, 2020

ZIO Clock MILLISECONDS and System.currentTimeMillis

 Scala Worksheet import java.util.concurrent.TimeUnit import zio.clock.Clock import zio.console.putStrLn import zio.console.Console import zio.duration.durationInt import zio.{ExitCode, Runtime, ZEnv, ZIO} val prg: ZIO[Console with Clock, Throwable, Int] = for { c <- ZIO.access[Clock](_.get) zio_begin <- c.currentTime(TimeUnit.MILLISECONDS) zio_beginM <- ZIO.accessM[Clock](_.get.currentTime(TimeUnit.MILLISECONDS)) sys_begin <- ZIO.succeed(System.currentTimeMillis) _ <- putStrLn(s" zio_begin = $zio_begin") _ <- putStrLn(s" zio_beginM = $zio_beginM") _ <- putStrLn(s" sys_begin = $sys_begin") _ <- c.sleep(1.second) zio_end <- c.currentTime(TimeUnit.MILLISECONDS) zio_endM <- ZIO.accessM[Clock](_.get.currentTime(TimeUnit.MILLISECONDS)) sys_end <- ZIO.succeed(System.currentTimeMillis) _ <- putStrLn(s" zio_end = $zio_end") _ <- putStrLn(s" zio_endM = $zio_beginM") _ <- putStrL

Variance

A good example, the book "Programming Scala" by Dean Wampler & Alex Payne.  Page 283 trait Function2[-T1, -T2, +R] extends AnyRef The last type parameter, +R, the is the return type. It is covariant. The leading two type parameters are for the first and second function arguments, respectively. They are contravariant.   class CSuper { def msuper() = println("CSuper") } class C extends CSuper { def m() = println("C") } class CSub extends C { def msub() = println("CSub") } var f: C => C = (c: C) => new C // f = (c: CSuper) => new CSub // c: CSuper is valid, because the argument // C is contravariant, so CSuper is a valid substitution // while the return value is covariant, so CSub is a valid replacement for C. f = (c: CSuper) => new C // Similar to the previous case, but we simply return a C. f = (c: C) => new CSub // Similar to the previous cases, but we sim