Skip to content

Print methods of the Console effect should not depend on Abort[IOException] - #1069

Merged
fwbrasil merged 5 commits into
getkyo:mainfrom
rcardin:remove-ioexception-from-console-print
Feb 5, 2025
Merged

fwbrasil merged 5 commits into
getkyo:mainfrom
rcardin:remove-ioexception-from-console-print

Conversation

@rcardin

@rcardin rcardin commented Feb 5, 2025

Copy link
Copy Markdown
Contributor

Fixes #1037

Problem

The Scala Console.println doesn't throw an IOException. The method uses the Java PrintStream type, which fails silently in case of errors. So, the kyo.Console effect should not depend upon an Abort[IOException] for printing text on the console.

We should rewrite the code such that the examples in the documentation will change accordingly:

val b: Unit < IO = Console.print("ok")

// Print to stdout with a new line
val c: Unit < IO = Console.printLine("ok")

// Print to stderr
val d: Unit < IO = Console.printErr("fail")

// Print to stderr with a new line
val e: Unit < IO = Console.printLineErr("fail")

// Explicitly specifying the 'Console' implementation
val f: Unit < IO = Console.let(Console.live)(e)

See PrintWriter and PrintStream never throw IOExceptions for further details.

Solution

I removed the Abort[IOException] effect from the return type of the kyo.Console methods concerning printing. Moreover, I added the following method, which returns whether the output or the error stream behind the scala.Console encountered an IOException error:

def checkErrors(using Frame): Boolean < IO

@@ -19,34 +19,41 @@ final case class Console(unsafe: Console.Unsafe):
* @param s

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you also update the top Console scaladoc with the rationale for not having a pending abort + the role of checkErrors? I think it's important to clearly flag this behavior to the user.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. Let me know if the text works for you.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

perfect! thanks

@rcardin
rcardin requested a review from fwbrasil February 5, 2025 20:28
@fwbrasil
fwbrasil merged commit cdf9406 into getkyo:main Feb 5, 2025
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.

Print methods of the Console effect should not depend on Abort[IOException]

2 participants