-
Couldn't load subscription status.
- Fork 1.4k
Implement ZIO#debug and related methods #4818
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
Conversation
|
I think these should probably use the live console so you don't have to change your method signatures when you use them for debugging purposes. |
|
Other question is whether we want it to be configurable regarding whether it prints and want to use a macro so we can eliminate the code if it isn't enabled. |
Oh yeah. That's a great idea. 😄 I'll do this.
I've seen some examples of this with Scala 3, but I'm not quite sure how to achieve it within the context of a library. Not sure where we'd put the |
32b92f8 to
d320c68
Compare
| * Taps the effect, printing the result of calling `.toString` on the value | ||
| */ | ||
| final def debug: ZIO[R, E, A] = | ||
| self.tapBoth( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would how we should handle a defect here? It seems like it could be useful for debugging but the full cause can be extremely long. Maybe we could just show the first cause in that case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like?
/**
* Taps the effect, printing the result of calling `.toString` on the value
* or the error.
*/
final def debug: ZIO[R, E, A] =
self
.tapCause(cause =>
cause.find {
case Cause.Interrupt(fiberId) => s"<INTERRUPT> by $fiberId"
case Cause.Die(throwable) => s"<DIE> $throwable"
case Cause.Fail(error) => s"<FAIL> $error"
}.fold(UIO.unit) { message =>
UIO(println(message))
}
)
.tap(value => UIO(println(value)))
/**
* Taps the effect, printing the result of calling `.toString` on the value.
* Prefixes the output with the given message.
*/
final def debug(prefix: => String): ZIO[R, E, A] =
self
.tapCause(cause =>
cause.find {
case Cause.Interrupt(fiberId) => s"$prefix: <INTERRUPT> by $fiberId"
case Cause.Die(throwable) => s"$prefix: <DIE> $throwable"
case Cause.Fail(error) => s"$prefix: <FAIL> $error"
}.fold(UIO.unit) { message =>
UIO(println(message))
}
)
.tap(value => UIO(println(s"$prefix: $value")))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I wouldn't mind not keeping it as it is, and letting the runtime render dies.
|
Yes, I'm not sure either. We can always optimize that later. |
A useful method for debugging/teaching/learning.
putStrLnis a bit difficult to use in an ad hoc manner as it requires a string, so you have to write.tap(arg => console.putStrLn(a.toString)), which is a bit of a mouthful when you just want to peek at the current value.I've implemented
debuganddebug(prefix: String)and related methods for easily printing out the value. All it does istapandputStrLnafter callingtoString, also optionally prefixing the output with the specified message.This will output: