-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Bracket example to doc #962
Conversation
docs/datatypes/io.md
Outdated
@@ -161,3 +161,27 @@ val action: Task[String] = Task.effectTotal(i += 1) *> Task.fail(new Throwable(" | |||
val cleanupAction: UIO[Unit] = UIO.effectTotal(i -= 1) | |||
val composite = action.ensuring(cleanupAction) | |||
``` | |||
### A full working example on using brackets | |||
```scala |
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.
When using the scala
lang directive, the snippet does not get typechecked, so we can't maintain its correctness over time.
If you switch this to scala mdoc:silent
as the other snippets above, it'll be typechecked.
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.
What @iravid said :)
Yeah, thank you, @iravid . I'll make a fix soon |
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.
Updated my directive as you advised. Thank you
898f033
to
3cf511d
Compare
docs/datatypes/io.md
Outdated
val string = Task.effect(new FileInputStream(file)).bracket(closeStream)(convertBytes).orDie | ||
|
||
// Print effect | ||
rt.unsafeRun(string) |
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.
Please refactor the example to not use rt.unsafeRun
, as that is not something we encourage.
In fact, since the example is using App
, you don't need to instantiate a runtime at all.
docs/datatypes/io.md
Outdated
mybracket.orDie.map(_ => 0) | ||
|
||
def closeStream(is: FileInputStream) = | ||
UIO.effectTotal(is.close()) |
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.
This can just be UIO(is.close())
docs/datatypes/io.md
Outdated
|
||
// run my bracket | ||
def run(args: List[String]) = | ||
mybracket.orDie.map(_ => 0) |
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.
mybracket.orDie.map(_ => 0) | |
mybracket.orDie.const(0) |
All Fixed. Pls review again |
docs/datatypes/io.md
Outdated
// Print effect | ||
rt.unsafeRun(string) | ||
|
||
unsafeRun(string) |
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.
We don't encourage using unsafeRun
directly, and it's not needed here as the App
will run the effect for you.
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.
e.g.
for {
file <- Task(new File("/tmp/hello"))
string <- Task(new FileInputStream(file)).bracket(closeStream)(convertBytes)
} yield string
docs/datatypes/io.md
Outdated
|
||
def convertBytes(is: FileInputStream) = | ||
Task.effect(println(new String(is.readAllBytes(), StandardCharsets.UTF_8))) | ||
Task.effect(println(new String(is.readAllBytes(), StandardCharsets.UTF_8))) // Java 11+ |
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.
We don't build yet on Java 11, so this probably won't typecheck.
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.
Fixed
This may be an issue. I run my code for Java 11 seamlessly. The very same code fails to compile is.readAllBytes()
on Java8. What to do? I don't know how to refactor readAllBytes
docs/datatypes/io.md
Outdated
// run my bracket | ||
def run(args: List[String]) = | ||
mybracket.orDie.map(_ => 0) | ||
mybracket.orDie.const(-1) // return -1 on fail |
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.
The -1
would be returned on the success cases, so you want to return 0
here.
docs/datatypes/io.md
Outdated
UIO(is.close()) | ||
|
||
def convertBytes(is: FileInputStream) = | ||
Task.effect(println(new String(is.readAllBytes(), StandardCharsets.UTF_8))) // Java 11+, since is.realAllBytes() won't compile on Java8 |
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.
Since this won't work on Java 8, I'd suggest writing a helper, a private method inside convertBytes
:
def readAll(fis: FileInputStream, len: Long): Array[Byte] = {
val content = Array.ofDim(len)
fis.read(content)
content
}
You can get the length of the file using new File(path).size
or something similar (can't remember it off the top of my head).
Added a helper method from @jdegoes. Now works fine on both Java8 and Java 11. Tested with ZIO 1.0.0-RC8-12 |
@tampler Did you push your changes? This looks the same as before... |
Yeah, now pushed. Thanks for your feedback |
@tampler This looks ready to merge to me! I noticed that mdoc is still failing. |
@jdegoes Yes, it fails on exit. That's what I get every time I launch a ZIO App. It says the following:
I don't know what is the reason of such behav. The code works okay, but returns an exception on each run. Any ideas? |
@tampler Here is the error that I see:
|
I am not sure if you can put |
Removed package from code |
@tampler Thanks for this full example! I'd love to get other full examples in for each major feature. |
No description provided.