Skip to content
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

Merged
merged 20 commits into from
Jul 10, 2019
Merged

Bracket example to doc #962

merged 20 commits into from
Jul 10, 2019

Conversation

tampler
Copy link
Contributor

@tampler tampler commented Jun 4, 2019

No description provided.

@@ -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
Copy link
Member

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.

Copy link
Member

@NeQuissimus NeQuissimus left a comment

Choose a reason for hiding this comment

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

What @iravid said :)

@tampler
Copy link
Contributor Author

tampler commented Jun 5, 2019

Yeah, thank you, @iravid . I'll make a fix soon

Copy link
Contributor Author

@tampler tampler left a 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

@NeQuissimus NeQuissimus force-pushed the master branch 2 times, most recently from 898f033 to 3cf511d Compare June 9, 2019 19:36
val string = Task.effect(new FileInputStream(file)).bracket(closeStream)(convertBytes).orDie

// Print effect
rt.unsafeRun(string)
Copy link
Member

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.

mybracket.orDie.map(_ => 0)

def closeStream(is: FileInputStream) =
UIO.effectTotal(is.close())
Copy link
Member

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())


// run my bracket
def run(args: List[String]) =
mybracket.orDie.map(_ => 0)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
mybracket.orDie.map(_ => 0)
mybracket.orDie.const(0)

@tampler
Copy link
Contributor Author

tampler commented Jun 15, 2019

All Fixed. Pls review again

// Print effect
rt.unsafeRun(string)

unsafeRun(string)
Copy link
Member

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.

Copy link
Member

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


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+
Copy link
Member

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.

Copy link
Contributor Author

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

// run my bracket
def run(args: List[String]) =
mybracket.orDie.map(_ => 0)
mybracket.orDie.const(-1) // return -1 on fail
Copy link
Member

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.

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
Copy link
Member

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).

@tampler
Copy link
Contributor Author

tampler commented Jun 22, 2019

Added a helper method from @jdegoes. Now works fine on both Java8 and Java 11. Tested with ZIO 1.0.0-RC8-12

@jdegoes
Copy link
Member

jdegoes commented Jun 23, 2019

@tampler Did you push your changes? This looks the same as before...

@tampler
Copy link
Contributor Author

tampler commented Jun 23, 2019

Yeah, now pushed. Thanks for your feedback

@jdegoes
Copy link
Member

jdegoes commented Jul 9, 2019

@tampler This looks ready to merge to me!

I noticed that mdoc is still failing.

@tampler
Copy link
Contributor Author

tampler commented Jul 9, 2019

@jdegoes Yes, it fails on exit. That's what I get every time I launch a ZIO App. It says the following:

Exception: sbt.TrapExitSecurityException thrown from the UncaughtExceptionHandler in thread "run-main-0"

I don't know what is the reason of such behav. The code works okay, but returns an exception on each run. Any ideas?

@jdegoes
Copy link
Member

jdegoes commented Jul 9, 2019

@tampler Here is the error that I see:

info: Compiling 48 files to /home/circleci/project/zio-docs/target/mdoc
error: /home/circleci/project/docs/datatypes/io.md:167:1: illegal start of definition
package hello
^^^^^^^

@jdegoes
Copy link
Member

jdegoes commented Jul 9, 2019

I am not sure if you can put package in an mdoc?

@tampler
Copy link
Contributor Author

tampler commented Jul 10, 2019

Removed package from code

@jdegoes jdegoes merged commit 6180669 into zio:master Jul 10, 2019
@jdegoes
Copy link
Member

jdegoes commented Jul 10, 2019

@tampler Thanks for this full example! I'd love to get other full examples in for each major feature.

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.

None yet

4 participants