-
Notifications
You must be signed in to change notification settings - Fork 50
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
Conversation
|
||
/** Represents a console for input and output operations. | ||
*/ | ||
abstract class Console: | ||
def unsafe: Console.Unsafe | ||
final case class Console(unsafe: Console.Unsafe) extends AnyVal: |
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.
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())) |
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.
Added tracking of IOException
.
f8235b3
to
a46f774
Compare
a46f774
to
4548d95
Compare
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)) |
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.
Will this catch only the type in the signature?
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.
Yes, the exception type is inferred but I've added explicit type params to avoid ambiguity
Result.catching(Maybe(scala.Console.in.readLine())).flatMap { | ||
case Absent => Result.fail(new EOFException("Consoles.readln failed.")) | ||
case Present(value) => Result.success(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.
Maybe
could have a method toSuccess[E](error: => E)
?
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.
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 |
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.
Will this actually output?
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.
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) = |
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.
withCapturedOut
?
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 think I prefer the shorter version
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 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
- https://blog.sunfishcode.online/bugs-in-hello-world/
- https://github.com/sunfishcode/hello-world-vs-io-errors
?
No, 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 Java's |
No description provided.