Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core-tests/shared/src/test/scala/zio/ZIOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3244,7 +3244,9 @@ object ZIOSpec extends ZIOBaseSpec {
} yield n
"""
}
val expected = "Cannot prove that NoSuchElementException <:< String."

val expected =
"Pattern guards are only supported when the error type is a supertype of NoSuchElementException. However, your effect has String for the error type."
if (TestVersion.isScala2) assertM(result)(isLeft(equalTo(expected)))
else assertM(result)(isLeft(anything))
}
Expand Down
19 changes: 17 additions & 2 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package zio

import scala.annotation.implicitNotFound
import scala.concurrent.ExecutionContext
import scala.reflect.ClassTag
import scala.util.{ Failure, Success }
Expand Down Expand Up @@ -3693,6 +3694,20 @@ object ZIO extends ZIOCompanionPlatformSpecific {
self.provideLayer[E1, R0, R0 with R1](ZLayer.identity[R0] ++ layer)
}

@implicitNotFound(
"Pattern guards are only supported when the error type is a supertype of NoSuchElementException. However, your effect has ${E} for the error type."
)
sealed trait CanFilter[+E] {
def apply(t: NoSuchElementException): E
}

object CanFilter {
implicit def canFilter[E >: NoSuchElementException]: CanFilter[E] =
new CanFilter[E] {
def apply(t: NoSuchElementException): E = t
}
}

implicit final class ZIOWithFilterOps[R, E, A](private val self: ZIO[R, E, A]) extends AnyVal {

/**
Expand All @@ -3704,10 +3719,10 @@ object ZIO extends ZIOCompanionPlatformSpecific {
* positive <- io2 if positive > 0
* } yield ()
*/
def withFilter(predicate: A => Boolean)(implicit ev: NoSuchElementException <:< E): ZIO[R, E, A] =
def withFilter(predicate: A => Boolean)(implicit ev: CanFilter[E]): ZIO[R, E, A] =
self.flatMap { a =>
if (predicate(a)) ZIO.succeedNow(a)
else ZIO.fail(new NoSuchElementException("The value doesn't satisfy the predicate"))
else ZIO.fail(ev(new NoSuchElementException("The value doesn't satisfy the predicate")))
}
}

Expand Down