Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Type Inference Issue with ZIO.whenM and ZIO.unlessM #3720

Merged
merged 1 commit into from May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Expand Up @@ -2966,6 +2966,17 @@ object ZIOSpec extends ZIOBaseSpec {
assert(conditionVal2)(equalTo(2)) &&
assert(failed)(isLeft(equalTo(failure)))
}
},
testM("infers correctly") {
trait R
trait R1 extends R
trait E1
trait E extends E1
trait A
val b: ZIO[R, E, Boolean] = ZIO.succeed(true)
val zio: ZIO[R1, E1, A] = ZIO.succeed(new A {})
val _ = ZIO.unlessM(b)(zio)
ZIO.succeed(assertCompletes)
}
),
suite("unrefine")(
Expand Down Expand Up @@ -3192,6 +3203,17 @@ object ZIOSpec extends ZIOBaseSpec {
assert(conditionVal2)(equalTo(2)) &&
assert(failed)(isLeft(equalTo(failure)))
}
},
testM("infers correctly") {
trait R
trait R1 extends R
trait E1
trait E extends E1
trait A
val b: ZIO[R, E, Boolean] = ZIO.succeed(true)
val zio: ZIO[R1, E1, A] = ZIO.succeed(new A {})
val _ = ZIO.whenM(b)(zio)
ZIO.succeed(assertCompletes)
}
),
suite("withFilter")(
Expand Down
10 changes: 5 additions & 5 deletions core/shared/src/main/scala/zio/IO.scala
Expand Up @@ -518,7 +518,7 @@ object IO {
* @see [[zio.ZIO.ifM]]
*/
def ifM[E](b: IO[E, Boolean]): ZIO.IfM[Any, E] =
new ZIO.IfM(b)
ZIO.ifM(b)

/**
* @see See [[zio.ZIO.interrupt]]
Expand Down Expand Up @@ -759,8 +759,8 @@ object IO {
/**
* @see See [[zio.ZIO.unlessM]]
*/
def unlessM[E](b: IO[E, Boolean])(zio: => IO[E, Any]): IO[E, Unit] =
ZIO.unlessM(b)(zio)
def unlessM[E](b: IO[E, Boolean]): ZIO.UnlessM[Any, E] =
ZIO.unlessM(b)

/**
* @see See [[zio.ZIO.unsandbox]]
Expand Down Expand Up @@ -817,8 +817,8 @@ object IO {
/**
* @see See [[zio.ZIO.whenM]]
*/
def whenM[E](b: IO[E, Boolean])(io: => IO[E, Any]): IO[E, Unit] =
ZIO.whenM(b)(io)
def whenM[E](b: IO[E, Boolean]): ZIO.WhenM[Any, E] =
ZIO.whenM(b)

/**
* @see See [[zio.ZIO.yieldNow]]
Expand Down
10 changes: 5 additions & 5 deletions core/shared/src/main/scala/zio/RIO.scala
Expand Up @@ -549,7 +549,7 @@ object RIO {
* @see [[zio.ZIO.ifM]]
*/
def ifM[R](b: RIO[R, Boolean]): ZIO.IfM[R, Throwable] =
new ZIO.IfM(b)
ZIO.ifM(b)

/**
* @see [[zio.ZIO.infinity]]
Expand Down Expand Up @@ -841,8 +841,8 @@ object RIO {
/**
* @see See [[zio.ZIO.unlessM]]
*/
def unlessM[R](b: RIO[R, Boolean])(zio: => RIO[R, Any]): RIO[R, Unit] =
ZIO.unlessM(b)(zio)
def unlessM[R](b: RIO[R, Boolean]): ZIO.UnlessM[R, Throwable] =
ZIO.unlessM(b)

/**
* @see See [[zio.ZIO.unsandbox]]
Expand Down Expand Up @@ -875,8 +875,8 @@ object RIO {
/**
* @see See [[zio.ZIO.whenM]]
*/
def whenM[R](b: RIO[R, Boolean])(rio: => RIO[R, Any]): RIO[R, Unit] =
ZIO.whenM(b)(rio)
def whenM[R](b: RIO[R, Boolean]): ZIO.WhenM[R, Throwable] =
ZIO.whenM(b)

/**
* @see See [[zio.ZIO.yieldNow]]
Expand Down
10 changes: 5 additions & 5 deletions core/shared/src/main/scala/zio/Task.scala
Expand Up @@ -518,7 +518,7 @@ object Task extends TaskPlatformSpecific {
* @see [[zio.ZIO.ifM]]
*/
def ifM(b: Task[Boolean]): ZIO.IfM[Any, Throwable] =
new ZIO.IfM(b)
ZIO.ifM(b)

/**
* @see See [[zio.ZIO.interrupt]]
Expand Down Expand Up @@ -754,8 +754,8 @@ object Task extends TaskPlatformSpecific {
/**
* @see See [[zio.ZIO.unlessM]]
*/
def unlessM(b: Task[Boolean])(zio: => Task[Any]): Task[Unit] =
ZIO.unlessM(b)(zio)
def unlessM(b: Task[Boolean]): ZIO.UnlessM[Any, Throwable] =
ZIO.unlessM(b)

/**
* @see [[zio.ZIO.unsandbox]]
Expand Down Expand Up @@ -788,8 +788,8 @@ object Task extends TaskPlatformSpecific {
/**
* @see See [[zio.ZIO.whenM]]
*/
def whenM(b: Task[Boolean])(task: => Task[Any]): Task[Unit] =
ZIO.whenM(b)(task)
def whenM(b: Task[Boolean]): ZIO.WhenM[Any, Throwable] =
ZIO.whenM(b)

/**
* @see See [[zio.ZIO.yieldNow]]
Expand Down
10 changes: 5 additions & 5 deletions core/shared/src/main/scala/zio/UIO.scala
Expand Up @@ -458,7 +458,7 @@ object UIO {
* @see [[zio.ZIO.ifM]]
*/
def ifM(b: UIO[Boolean]): ZIO.IfM[Any, Nothing] =
new ZIO.IfM(b)
ZIO.ifM(b)

/**
* @see See [[zio.ZIO.interrupt]]
Expand Down Expand Up @@ -668,8 +668,8 @@ object UIO {
/**
* @see See [[zio.ZIO.unlessM]]
*/
def unlessM(b: UIO[Boolean])(zio: => UIO[Any]): UIO[Unit] =
ZIO.unlessM(b)(zio)
def unlessM(b: UIO[Boolean]): ZIO.UnlessM[Any, Nothing] =
ZIO.unlessM(b)

/**
* @see [[zio.ZIO.unsandbox]]
Expand Down Expand Up @@ -702,8 +702,8 @@ object UIO {
/**
* @see See [[zio.ZIO.whenM]]
*/
def whenM(b: UIO[Boolean])(uio: => UIO[Any]): UIO[Unit] =
ZIO.whenM(b)(uio)
def whenM(b: UIO[Boolean]): ZIO.WhenM[Any, Nothing] =
ZIO.whenM(b)

/**
* @see See [[zio.ZIO.yieldNow]]
Expand Down
9 changes: 5 additions & 4 deletions core/shared/src/main/scala/zio/URIO.scala
Expand Up @@ -492,7 +492,7 @@ object URIO {
* @see [[zio.ZIO.ifM]]
*/
def ifM[R](b: URIO[R, Boolean]): ZIO.IfM[R, Nothing] =
new ZIO.IfM(b)
ZIO.ifM(b)

/**
* @see [[zio.ZIO.infinity]]
Expand Down Expand Up @@ -757,8 +757,8 @@ object URIO {
/**
* @see See [[zio.ZIO.unlessM]]
*/
def unlessM[R](b: URIO[R, Boolean])(zio: => URIO[R, Any]): URIO[R, Unit] =
ZIO.unlessM(b)(zio)
def unlessM[R](b: URIO[R, Boolean]): ZIO.UnlessM[R, Nothing] =
ZIO.unlessM(b)

/**
* @see [[zio.ZIO.unsandbox]]
Expand Down Expand Up @@ -790,7 +790,8 @@ object URIO {
/**
* @see [[zio.ZIO.whenM]]
*/
def whenM[R](b: URIO[R, Boolean])(rio: => URIO[R, Any]): URIO[R, Unit] = ZIO.whenM(b)(rio)
def whenM[R](b: URIO[R, Boolean]): ZIO.WhenM[R, Nothing] =
ZIO.whenM(b)

/**
* @see [[zio.ZIO.yieldNow]]
Expand Down
18 changes: 14 additions & 4 deletions core/shared/src/main/scala/zio/ZIO.scala
Expand Up @@ -3521,8 +3521,8 @@ object ZIO extends ZIOCompanionPlatformSpecific {
/**
* The moral equivalent of `if (!p) exp` when `p` has side-effects
*/
def unlessM[R, E](b: ZIO[R, E, Boolean])(zio: => ZIO[R, E, Any]): ZIO[R, E, Unit] =
b.flatMap(b => if (b) unit else zio.unit)
def unlessM[R, E](b: ZIO[R, E, Boolean]): ZIO.UnlessM[R, E] =
new ZIO.UnlessM(b)

/**
* The inverse operation `IO.sandboxed`
Expand Down Expand Up @@ -3610,8 +3610,8 @@ object ZIO extends ZIOCompanionPlatformSpecific {
/**
* The moral equivalent of `if (p) exp` when `p` has side-effects
*/
def whenM[R, E](b: ZIO[R, E, Boolean])(zio: => ZIO[R, E, Any]): ZIO[R, E, Unit] =
b.flatMap(b => if (b) zio.unit else unit)
def whenM[R, E](b: ZIO[R, E, Boolean]): ZIO.WhenM[R, E] =
new ZIO.WhenM(b)

/**
* Returns an effect that yields to the runtime system, starting on a fresh
Expand Down Expand Up @@ -3707,6 +3707,16 @@ object ZIO extends ZIOCompanionPlatformSpecific {
b.flatMap(b => if (b) onTrue else onFalse)
}

final class UnlessM[R, E](private val b: ZIO[R, E, Boolean]) extends AnyVal {
def apply[R1 <: R, E1 >: E](zio: => ZIO[R1, E1, Any]): ZIO[R1, E1, Unit] =
b.flatMap(b => if (b) unit else zio.unit)
}

final class WhenM[R, E](private val b: ZIO[R, E, Boolean]) extends AnyVal {
def apply[R1 <: R, E1 >: E](zio: => ZIO[R1, E1, Any]): ZIO[R1, E1, Unit] =
b.flatMap(b => if (b) zio.unit else unit)
}

final class TimeoutTo[-R, +E, +A, +B](self: ZIO[R, E, A], b: B) {
def apply[B1 >: B](f: A => B1)(duration: Duration): ZIO[R with Clock, E, B1] =
(self map f) raceFirst (ZIO.sleep(duration).interruptible as b)
Expand Down
18 changes: 14 additions & 4 deletions core/shared/src/main/scala/zio/ZManaged.scala
Expand Up @@ -1147,6 +1147,16 @@ object ZManaged {
self.provideLayer[E1, R0, R0 with R1](ZLayer.identity[R0] ++ layer)
}

final class UnlessM[R, E](private val b: ZManaged[R, E, Boolean]) extends AnyVal {
def apply[R1 <: R, E1 >: E](managed: => ZManaged[R1, E1, Any]): ZManaged[R1, E1, Unit] =
b.flatMap(b => if (b) unit else managed.unit)
}

final class WhenM[R, E](private val b: ZManaged[R, E, Boolean]) extends AnyVal {
def apply[R1 <: R, E1 >: E](managed: => ZManaged[R1, E1, Any]): ZManaged[R1, E1, Unit] =
b.flatMap(b => if (b) managed.unit else unit)
}

/**
* A `ReleaseMap` represents the finalizers associated with a scope.
*
Expand Down Expand Up @@ -2203,8 +2213,8 @@ object ZManaged {
/**
* The moral equivalent of `if (!p) exp` when `p` has side-effects
*/
def unlessM[R, E](b: ZManaged[R, E, Boolean])(zio: => ZManaged[R, E, Any]): ZManaged[R, E, Unit] =
b.flatMap(b => if (b) unit else zio.unit)
def unlessM[R, E](b: ZManaged[R, E, Boolean]): ZManaged.UnlessM[R, E] =
new ZManaged.UnlessM(b)

/**
* The inverse operation to `sandbox`. Submerges the full cause of failure.
Expand Down Expand Up @@ -2241,8 +2251,8 @@ object ZManaged {
/**
* The moral equivalent of `if (p) exp` when `p` has side-effects
*/
def whenM[R, E](b: ZManaged[R, E, Boolean])(zManaged: => ZManaged[R, E, Any]): ZManaged[R, E, Unit] =
b.flatMap(b => if (b) zManaged.unit else unit)
def whenM[R, E](b: ZManaged[R, E, Boolean]): ZManaged.WhenM[R, E] =
new ZManaged.WhenM(b)

private[zio] def succeedNow[A](r: A): ZManaged[Any, Nothing, A] =
ZManaged(IO.succeedNow((Finalizer.noop, r)))
Expand Down
9 changes: 5 additions & 4 deletions core/shared/src/main/scala/zio/stm/STM.scala
Expand Up @@ -186,7 +186,7 @@ object STM {
* @see See [[zio.stm.ZSTM.ifM]]
*/
def ifM[E](b: STM[E, Boolean]): ZSTM.IfM[Any, E] =
new ZSTM.IfM(b)
ZSTM.ifM(b)

/**
* @see See [[zio.stm.ZSTM.iterate]]
Expand Down Expand Up @@ -324,8 +324,8 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.unlessM]]
*/
def unlessM[E](b: STM[E, Boolean])(stm: => STM[E, Any]): STM[E, Unit] =
ZSTM.unlessM(b)(stm)
def unlessM[E](b: STM[E, Boolean]): ZSTM.UnlessM[Any, E] =
ZSTM.unlessM(b)

/**
* @see See [[zio.stm.ZSTM.validate]]
Expand Down Expand Up @@ -363,7 +363,8 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.whenM]]
*/
def whenM[E](b: STM[E, Boolean])(stm: => STM[E, Any]): STM[E, Unit] = ZSTM.whenM(b)(stm)
def whenM[E](b: STM[E, Boolean]): ZSTM.WhenM[Any, E] =
ZSTM.whenM(b)

private[zio] def succeedNow[A](a: A): USTM[A] =
ZSTM.succeedNow(a)
Expand Down
18 changes: 14 additions & 4 deletions core/shared/src/main/scala/zio/stm/ZSTM.scala
Expand Up @@ -1436,8 +1436,8 @@ object ZSTM {
/**
* The moral equivalent of `if (!p) exp` when `p` has side-effects
*/
def unlessM[R, E](b: ZSTM[R, E, Boolean])(stm: => ZSTM[R, E, Any]): ZSTM[R, E, Unit] =
b.flatMap(b => if (b) unit else stm.unit)
def unlessM[R, E](b: ZSTM[R, E, Boolean]): ZSTM.UnlessM[R, E] =
new ZSTM.UnlessM(b)

/**
* Feeds elements of type `A` to `f` and accumulates all errors in error
Expand Down Expand Up @@ -1484,8 +1484,8 @@ object ZSTM {
/**
* The moral equivalent of `if (p) exp` when `p` has side-effects
*/
def whenM[R, E](b: ZSTM[R, E, Boolean])(stm: => ZSTM[R, E, Any]): ZSTM[R, E, Unit] =
b.flatMap(b => if (b) stm.unit else unit)
def whenM[R, E](b: ZSTM[R, E, Boolean]): ZSTM.WhenM[R, E] =
new ZSTM.WhenM(b)

private[zio] def succeedNow[A](a: A): USTM[A] =
succeed(a)
Expand All @@ -1505,6 +1505,16 @@ object ZSTM {
b.flatMap(b => if (b) onTrue else onFalse)
}

final class UnlessM[R, E](private val b: ZSTM[R, E, Boolean]) {
def apply[R1 <: R, E1 >: E](stm: => ZSTM[R1, E1, Any]): ZSTM[R1, E1, Unit] =
b.flatMap(b => if (b) unit else stm.unit)
}

final class WhenM[R, E](private val b: ZSTM[R, E, Boolean]) {
def apply[R1 <: R, E1 >: E](stm: => ZSTM[R1, E1, Any]): ZSTM[R1, E1, Unit] =
b.flatMap(b => if (b) stm.unit else unit)
}

private final class Resumable[E, E1, A, B](
val stm: STM[E, A],
val ks: Stack[internal.TExit[E, A] => STM[E1, B]]
Expand Down