svroonland/rezilience

Improve type inference for composing policies

Closed this issue · 3 comments

Right now it requires a lot of type parameters to make it compile, eg

def common[E](
    rateLimiter: RateLimiter = noopRateLimiter,
    bulkhead: Bulkhead = noopBulkhead,
    circuitBreaker: CircuitBreaker[PolicyError[E]] = noopCircuitBreaker,
    retry: Retry[E] = noopRetry[E]
  ): Policy[E, PolicyError[E]] = {
    val cb: Policy[PolicyError[E], PolicyError[E]] =
      circuitBreaker.toPolicy[PolicyError[E]].mapError(circuitBreakerErrorToPolicyError).mapError(flattenWrappedError)
    val b: Policy[E, PolicyError[E]]               =
      bulkhead.toPolicy[E].mapError(bulkheadErrorToPolicyError)

    val r = rateLimiter.toPolicy[PolicyError[E]]

    val retryPolicy = retry
      .widen[PolicyError[E]] { case WrappedError(e) => e }

    b compose cb compose r compose retryPolicy
  }
trait Policy[+LowerErr, -UpperErr] {
  def apply[R, E >: LowerErr<: UpperErr, A](zio: ZIO[R, E, A]): ZIO[R, E, A]
}

Now your policies that don't care about the error type (because they are polymorphic) will use Policy[Nothing, Any], which will "narrow" as appropriate when composed with other policies.

Great library and good luck! 💪

Thank you @jdegoes, I will have a go at that. And thanks for the mention on Twitter!

Improved in #35