8000 Fix semantics of `provide` and improve type inference by jdegoes · Pull Request #597 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix semantics of provide and improve type inference #597

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

Merged
merged 16 commits into from
Mar 6, 2019

Conversation

jdegoes
Copy link
Member
@jdegoes jdegoes commented Mar 3, 2019

This is mainly to fix semantics of provide which was incorrectly using "last one wins" instead of "scoped provision". However, I also improved type inference.

@jdegoes jdegoes requested a review from wi101 March 3, 2019 18:38
@@ -26,6 +26,14 @@ package object zio extends EitherCompat {
type Task[+A] = ZIO[Any, Throwable, A]
type UIO[+A] = ZIO[Any, Nothing, A]

implicit class ZIOInvarant[R, E, A](val self: ZIO[R, E, A]) extends AnyVal {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in Invariant

Also, I think that if you put it in the ZIO companion object, it won't need to be imported.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed both

@LGLO
Copy link
Contributor
LGLO commented Mar 3, 2019

I can't see how using ZIO.bracket doesn't allocate two objects: BracketAcquire and BracketRelease.
If my computation will use bracket in hot path (for example to acquire some Ref or Semaphore) then it will cause many allocations.

EDIT: I was thinking about implicit case class, like ZIOInvariant but together with phantom type but then I came to conclusion that it's not worth it.

@jdegoes
Copy link
Member Author
jdegoes commented Mar 3, 2019

@LGLO Did you see if we can fix by some other means? Sorry overnight 17 pull requests appeared and I am way behind on reviews. Will do more tomorrow.

My other idea is to have a ZIO.bracket which takes ALL parameters at once. Then the "syntax sugar" method simply delegates to this one. This way if you have it in a hot path, you just use the uncurried form...

@LGLO
Copy link
Contributor
LGLO commented Mar 3, 2019

@jdegoes I made this uncurried version but TBH I don't really like it if was the only version available. Having both looks good to me.

Is ZIOInvarant required? Why is it better than instance methods?

@jdegoes
Copy link
Member Author
jdegoes commented Mar 3, 2019

@LGLO Can you submit a pull request to my pull request? 😄

ZIOInvariant improves type inference by eliminating one layer of widening (if we put it onto ZIO directly, then we have to widen all parameters).

@LGLO
Copy link
Contributor
LGLO commented Mar 3, 2019 via email

Copy link
Member
@regiskuckaertz regiskuckaertz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bye bye type annotations 👋 Just a few questions but otherwise this looks great!

@@ -530,7 +530,7 @@ trait Stream[-R, +E, +A] extends Serializable { self =>
/**
* Adds an effect to consumption of every element of the stream.
*/
final def withEffect[R1 <: R, E1 >: E](f: A => ZIO[R1, E1, Unit]): Stream[R1, E1, A] =
final def tap[R1 <: R, E1 >: E](f: A => ZIO[R1, E1, Unit]): Stream[R1, E1, A] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f: A => ZIO[R1, E1, _]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍

@@ -66,8 +66,8 @@ trait StreamChunk[-R, +E, @specialized +A] { self =>
final def foreach[R1 <: R, E1 >: E](f: A => ZIO[R1, E1, Unit]): ZIO[R1, E1, Unit] =
foreachWhile[R1, E1](f(_).const(true))

final def withEffect[R1 <: R, E1 >: E](f0: A => ZIO[R1, E1, Unit]): StreamChunk[R1, E1, A] =
StreamChunk(chunks.withEffect[R1, E1] { as =>
final def tap[R1 <: R, E1 >: E](f0: A => ZIO[R1, E1, Unit]): StreamChunk[R1, E1, A] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f0: A => ZIO[R1, E1, _]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍

iravid
iravid previously approved these changes Mar 6, 2019
@@ -20,7 +20,7 @@ package scalaz.zio
* An `Exit[E, A]` describes the result of executing an `IO` value. The
* result is either succeeded with a value `A`, or failed with a `Cause[E]`.
*/
sealed abstract class Exit[+E, +A] extends Product with Serializable { self =>
sealed trait Exit[+E, +A] extends Product with Serializable { self =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, why the change? I thought abstract classes generate more efficient bytecode most of the time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dropped Scala 2.11. The main reason for abstract class is to avoid breaking backward binary compatibility on adding a method with a default implementation.

} yield l.succeed(()) *> p.await

/**
* Maps this effect to the specified constant while preserving the
* effects of this effect.
*/
final def const[B](b: => B): ZIO[R, E, B] = self.map(_ => b)
final def const[B](b: => B): ZIO[R, E, B] = self map (_ => b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OCD much 😂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have no idea 😆

ghostdogpr
ghostdogpr previously approved these changes Mar 6, 2019
@jdegoes jdegoes dismissed stale reviews from ghostdogpr and iravid via 42c6e5b March 6, 2019 12:17
@@ -765,20 +765,26 @@ object Sink {
}
}

final def seq[E1 >: E, C](that: Sink[R, E1, A, A, C]): Sink[R, E1, A, A, (B, C)] =
final def zip[E1 >: E, C](that: Sink[R, E1, A, A, C]): Sink[R, E1, A, A, (B, C)] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also allow narrowing of R here. I'll open a separate issue for this

@jdegoes jdegoes merged commit 69bac60 into zio:master Mar 6, 2019
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.

5 participants
0