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
Add Type Alias For Intersection Types On Scala 2 #4845
Conversation
Bloody hell that was fast 🕺 I refrained from going one step further because of the dotty compat but this would be nicer: type &[A, B] = Has[A] with Has[B] What do you think? I thought about this after @kitlangton 's presentation at zio world. I think it's a great idea to get rid of the type alias boilerplate but |
The following works in Dotty, but there's not much we can do in Scala 2. type ++[A,B] <: Has[_] = (A,B) match {
case (Has[_], Has[_]) => A & B & Has[_]
case (_, Has[_]) => Has[A] & B
case (Has[_], _) => A & Has[B]
case (_,_) => Has[A] & Has[B]
}
val layer = ZLayer.fromAuto[Console ++ String ++ Has[Int] ++ Boolean](layer, boolLayer, Console.live) |
@regiskuckaertz I think one of our lessons from ZIO 1.0 has been that hiding the |
That is really interesting regarding the match types. |
While I agree it could be nice to have something like that in Scala 2: type ++[A,B] = Has[A] with Has[B] The problem is when you use it to join more than two types, the left hand side is now a Has and will become doubly wrapped, so you'd actually need: type ++[A,B] = A with Has[B] But then it's a bit weird to use, as you have to explicitly wrap the first type—or add some second operator that also wraps the lhs. Either way, not too great. Probably best for individuals to implement it themselves in their own libraries if they really want that, until we can figure out something more elegant (like that match type approach). |
@kitlangton hmm, I see, at that point @adamgfraser good point, I think this is wise. |
Resolves #4844. Adds a type alias to allow
&
to be used on both Scala 2 and Scala 3.