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

[core] Console improvements #785

Merged
merged 4 commits into from
Oct 29, 2024
Merged

[core] Console improvements #785

merged 4 commits into from
Oct 29, 2024

Conversation

fwbrasil
Copy link
Collaborator

No description provided.


/** Represents a console for input and output operations.
*/
abstract class Console:
def unsafe: Console.Unsafe
final case class Console(unsafe: Console.Unsafe) extends AnyVal:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

update the pattern like in the other effects


/** Reads a line from the console.
*
* @return
* A String representing the line read from the console.
*/
def readln(using Frame): String < IO
def readln(using Frame): String < (IO & Abort[IOException]) = IO.Unsafe(Abort.get(unsafe.readln()))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added tracking of IOException.

@fwbrasil fwbrasil force-pushed the console-improvements branch 3 times, most recently from f8235b3 to a46f774 Compare October 28, 2024 06:08
@fwbrasil fwbrasil force-pushed the console-improvements branch from a46f774 to 4548d95 Compare October 28, 2024 06:19
case Absent => Result.fail(new EOFException("Consoles.readln failed."))
case Present(value) => Result.success(value)
}
def print(s: String)(using AllowUnsafe) = Result.catching(scala.Console.out.print(s))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this catch only the type in the signature?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, the exception type is inferred but I've added explicit type params to avoid ambiguity

Comment on lines 61 to 63
Result.catching(Maybe(scala.Console.in.readLine())).flatMap {
case Absent => Result.fail(new EOFException("Consoles.readln failed."))
case Present(value) => Result.success(value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe could have a method toSuccess[E](error: => E)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good idea! I've added maybe.toResult methods.

val stdErr = new StringBuffer
val proxy =
new Proxy(console.unsafe):
override def print(s: String)(using AllowUnsafe) = Result.success(stdOut.append(s)).unit
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this actually output?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, the idea is to only collect the output

* @return
* A tuple containing the captured output (Out) and the computation result.
*/
def withOut[A, S](v: A < S)(using Frame): (Out, A) < (IO & S) =
Copy link
Collaborator

Choose a reason for hiding this comment

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

withCapturedOut?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I prefer the shorter version

Copy link
Contributor

@sideeffffect sideeffffect left a comment

Choose a reason for hiding this comment

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

Since this PR is about Console and errors, I'd like to ask, whether Kyo is able to surface (instead of swallowing) the error(s) described in

?

@fwbrasil
Copy link
Collaborator Author

Since this PR is about Console and errors, I'd like to ask, whether Kyo is able to surface (instead of swallowing) the error(s) described in

?

No, we don't handle exit codes yet. Do you mind creating an issue for it?

@sideeffffect
Copy link
Contributor

sideeffffect commented Oct 29, 2024

we don't handle exit codes yet. Do you mind creating an issue for it?

Happy to help:

But the issue the blog post describes is even more about silently swallowing failures when writing to stream/pipe. So I thought it's relevant to this PR, which is changing things around Console. Does (Kyo) Console swallow errors like that? Because errors passing silently is a bad thing ™, right?

Java's PrintStream has a method checkError which is able to uncover such errors, albeit the method has to be called explicitly.

@hearnadam hearnadam merged commit c9f7e82 into main Oct 29, 2024
3 checks passed
@hearnadam hearnadam deleted the console-improvements branch October 29, 2024 15:10
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.

3 participants