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

Skip to content

Conversation

@adamgfraser
Copy link
Contributor

Resolves #3997.

There has been a desire by library authors to generalize over the types _ <: Has[_] and Any to describe conceptually "the set of all environmental requirements that can be combined generically".

This PR changes the encoding of Has.AreHas, which describes the capability to combine two types to form their intersection, from:

  trait AreHas[-R, -R1] {
    def union[R0 <: R, R00 <: R1: Tag](r: R0, r1: R00): R0 with R00

to:

  trait Union[R, R1] {
    def union(r: R, r1: R1): R with R1
  }

Instances are then provided for combinations of Has with Has, Has with Any, Any with Has, and Any with Any.

The biggest disadvantage of this proposal I can see is we lose the contravariance on the type parameters of R and R1. Previously, we could say that if R and R1 are subtypes of Has, then R0 <: R and R00 <: R1 are also subtypes of Has and so can be combined generically. But now if R is Any it is not the case that we can combine subtypes of R generically. This requires type annotations in the implementation of a couple of additional ZLayer combinators, but it only required one change to the API (adding an additional ROut1 >: ROut parameter in +!+ for variance) and no additional type annotations in any of the tests, so I think the impact will be limited to library authors who are writing their own combinators to combine environment types generically.

We don't gain a lot from this capability in our own library but it avoids the need for libraries like zio-grpc to implement it themselves and potentially provides them with a common way to do so.

Copying @thesamet.

@adamgfraser adamgfraser requested a review from jdegoes July 27, 2020 21:01
@jdegoes
Copy link
Member

jdegoes commented Jul 28, 2020

Good to merge when passing! 👍

@adamgfraser adamgfraser merged commit d02ab80 into zio:master Jul 28, 2020
@adamgfraser adamgfraser deleted the union branch July 28, 2020 03:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Union of Has[_] and Any

2 participants