Skip to content

Instantly share code, notes, and snippets.

@przemek-pokrywka
Created August 5, 2023 20:08
Show Gist options
  • Save przemek-pokrywka/5cc49ff691d36f08b0ef3ffc74b14553 to your computer and use it in GitHub Desktop.
Save przemek-pokrywka/5cc49ff691d36f08b0ef3ffc74b14553 to your computer and use it in GitHub Desktop.
Am I doing something wrong or substituting elements of environment does not work?
//> using dep "dev.zio::zio:2.0.15"
//> using scala 2.13
import zio._
import java.time
import java.time.temporal.ChronoUnit
import java.time.{Instant, LocalDateTime, OffsetDateTime}
import java.util.concurrent.TimeUnit
object CannotSubstituteMyClock {
def main(args: Array[String]): Unit =
Unsafe.unsafe { implicit unsafe =>
zio.Runtime.default.mapEnvironment(env => env.add[Clock](MyClock)).unsafe.runToFuture(
ZIO.clock.map(_.toString).debug("clock") *>
Clock.currentDateTime.debug("time")
)
}
}
object MyClock extends Clock {
override def currentTime(unit: => TimeUnit)(implicit trace: Trace): UIO[Long] = ???
override def currentTime(unit: => ChronoUnit)(implicit trace: Trace, d: DummyImplicit): UIO[Long] = ???
override def currentDateTime(implicit trace: Trace): UIO[OffsetDateTime] = ???
override def instant(implicit trace: Trace): UIO[Instant] = ???
override def javaClock(implicit trace: Trace): UIO[time.Clock] = ???
override def localDateTime(implicit trace: Trace): UIO[LocalDateTime] = ???
override def nanoTime(implicit trace: Trace): UIO[Long] = ???
override def scheduler(implicit trace: Trace): UIO[Scheduler] = ???
override def sleep(duration: => zio.Duration)(implicit trace: Trace): UIO[Unit] = ???
override def toString: String = "MyClock"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment