Skip to content

Commit

Permalink
[core] more clear Console method names (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil authored Dec 5, 2024
1 parent 46e002e commit 2c24337
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 67 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,11 @@ object MyApp extends KyoApp:
// field initialization issues.
run {
for
_ <- Console.println(s"Main args: $args")
_ <- Console.printLine(s"Main args: $args")
currentTime <- Clock.now
_ <- Console.println(s"Current time is: $currentTime")
_ <- Console.printLine(s"Current time is: $currentTime")
randomNumber <- Random.nextInt(100)
_ <- Console.println(s"Generated random number: $randomNumber")
_ <- Console.printLine(s"Generated random number: $randomNumber")
yield
// The produced value can be of any type and is
// automatically printed to the console.
Expand Down Expand Up @@ -969,7 +969,7 @@ val d: Int < IO =
val e: Int < (IO & Abort[IOException]) =
Loop(1)(i =>
if i < 5 then
Console.println(s"Iteration: $i").map(_ => Loop.continue(i + 1))
Console.printLine(s"Iteration: $i").map(_ => Loop.continue(i + 1))
else
Loop.done(i)
)
Expand Down Expand Up @@ -1020,7 +1020,7 @@ val a: Int < (IO & Abort[IOException]) =
Loop.indexed(1)((idx, i) =>
if idx < 10 then
if idx % 3 == 0 then
Console.println(s"Iteration $idx").map(_ => Loop.continue(i + 1))
Console.printLine(s"Iteration $idx").map(_ => Loop.continue(i + 1))
else
Loop.continue(i + 1)
else
Expand Down Expand Up @@ -1236,7 +1236,7 @@ val j: Int < Any =

// Process each element with side effects
val k: Unit < (IO & Abort[IOException]) =
a.runForeach(Console.println(_))
a.runForeach(Console.printLine(_))
```

Streams can be combined with other effects, allowing for powerful and flexible data processing pipelines:
Expand Down Expand Up @@ -1415,9 +1415,9 @@ val loggingCut =
[C] =>
(input, cont) =>
for
_ <- Console.println(s"Processing: $input")
_ <- Console.printLine(s"Processing: $input")
result <- cont(input)
_ <- Console.println(s"Result: $result")
_ <- Console.printLine(s"Result: $result")
yield result
)

Expand All @@ -1432,7 +1432,7 @@ val successExample: Unit < (Abort[Throwable] & IO) =
numberAspect.let(composedCut) {
process(5) // Will succeed: 5 * 2 -> 10
}
_ <- Console.println(s"Success result: $result")
_ <- Console.printLine(s"Success result: $result")
yield ()

// Failure case
Expand All @@ -1442,7 +1442,7 @@ val failureExample: Unit < (Abort[Throwable] & IO) =
numberAspect.let(composedCut) {
process(-3) // Will fail with Invalid("negative number")
}
_ <- Console.println("This won't be reached due to Abort")
_ <- Console.printLine("This won't be reached due to Abort")
yield ()
```

Expand Down Expand Up @@ -1482,9 +1482,9 @@ val loggingCut =
[C] =>
(input, cont) =>
for
_ <- Console.println(s"Processing request: ${input}")
_ <- Console.printLine(s"Processing request: ${input}")
result <- cont(input)
_ <- Console.println(s"Response: ${result}")
_ <- Console.printLine(s"Response: ${result}")
yield result
)

Expand All @@ -1507,7 +1507,7 @@ val example: Unit < (IO & Abort[Throwable]) =
serviceAspect.let(composedCut) {
processRequest(req2)
}
_ <- Console.println(s"Results: $r1, $r2")
_ <- Console.printLine(s"Results: $r1, $r2")
yield ()
```

Expand Down Expand Up @@ -1548,23 +1548,23 @@ import java.io.IOException

// Read a line from the console
val a: String < (IO & Abort[IOException]) =
Console.readln
Console.readLine

// Print to stdout
val b: Unit < (IO & Abort[IOException]) =
Console.print("ok")

// Print to stdout with a new line
val c: Unit < (IO & Abort[IOException]) =
Console.println("ok")
Console.printLine("ok")

// Print to stderr
val d: Unit < (IO & Abort[IOException]) =
Console.printErr("fail")

// Print to stderr with a new line
val e: Unit < (IO & Abort[IOException]) =
Console.printlnErr("fail")
Console.printLineErr("fail")

// Explicitly specifying the 'Console' implementation
val f: Unit < (IO & Abort[IOException]) =
Expand Down Expand Up @@ -1861,15 +1861,15 @@ val lines: Stream[String, Resource & IO] =

// Process the stream
val result: Unit < (Resource & Console & Async & Abort[IOException]) =
lines.map(line => Console.println(line)).runDiscard
lines.map(line => Console.printLine(line)).runDiscard

// Walk a directory tree
val tree: Stream[Path, IO] =
Path("tmp").walk

// Process each file in the tree
val processedTree: Unit < (Console & Async & Abort[IOException]) =
tree.map(file => file.read.map(content => Console.println(s"File: ${file}, Content: $content"))).runDiscard
tree.map(file => file.read.map(content => Console.printLine(s"File: ${file}, Content: $content"))).runDiscard
```

`Path` integrates with Kyo's `Stream` API, allowing for efficient processing of file contents using streams. The `sink` and `sinkLines` extension methods on `Stream` enable writing streams of data back to files.
Expand Down Expand Up @@ -1908,7 +1908,7 @@ import kyo.*

class MyClass extends KyoApp:
run {
Console.println(s"Executed with args: $args")
Console.printLine(s"Executed with args: $args")
}
end MyClass

Expand Down Expand Up @@ -3349,7 +3349,7 @@ val effect: Unit < (Console & Async & Resource & Abort[Throwable] & Env[NameServ
for
nameService <- Kyo.service[NameService] // Adds Env[NameService] effect
_ <- keepTicking.forkScoped // Adds Console, Async, and Resource effects
saluee <- Console.readln // Uses Console effect
saluee <- Console.readLine // Uses Console effect
_ <- Kyo.sleep(2.seconds) // Uses Async (semantic blocking)
_ <- nameService.sayHelloTo(saluee) // Adds Abort[Throwable] effect
yield ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ extension (kyoObject: Kyo.type)
* An effect that prints the message to the console
*/
def debugln(message: String)(using Frame): Unit < (IO & Abort[IOException]) =
Console.println(message)
Console.printLine(message)

/** Creates an effect that fails with Abort[E].
*
Expand Down
60 changes: 30 additions & 30 deletions kyo-core/shared/src/main/scala/kyo/Console.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final case class Console(unsafe: Console.Unsafe):
* @return
* A String representing the line read from the console.
*/
def readln(using Frame): String < (IO & Abort[IOException]) = IO.Unsafe(Abort.get(unsafe.readln()))
def readLine(using Frame): String < (IO & Abort[IOException]) = IO.Unsafe(Abort.get(unsafe.readLine()))

/** Prints a string to the console without a newline.
*
Expand All @@ -33,14 +33,14 @@ final case class Console(unsafe: Console.Unsafe):
* @param s
* The string to print.
*/
def println(s: String)(using Frame): Unit < (IO & Abort[IOException]) = IO.Unsafe(Abort.get(unsafe.println(s)))
def println(s: String)(using Frame): Unit < (IO & Abort[IOException]) = IO.Unsafe(Abort.get(unsafe.printLine(s)))

/** Prints a string to the console's error stream followed by a newline.
*
* @param s
* The string to print to the error stream.
*/
def printlnErr(s: String)(using Frame): Unit < (IO & Abort[IOException]) = IO.Unsafe(Abort.get(unsafe.printlnErr(s)))
def printLineErr(s: String)(using Frame): Unit < (IO & Abort[IOException]) = IO.Unsafe(Abort.get(unsafe.printLineErr(s)))

/** Flushes the console output streams.
*
Expand All @@ -57,14 +57,14 @@ object Console:
*/
val live: Console = Console(
new Unsafe:
def readln()(using AllowUnsafe) =
def readLine()(using AllowUnsafe) =
Result.catching[IOException](Maybe(scala.Console.in.readLine()))
.flatMap(_.toResult(Result.fail(new EOFException("Consoles.readln failed."))))
def print(s: String)(using AllowUnsafe) = Result.catching[IOException](scala.Console.out.print(s))
def printErr(s: String)(using AllowUnsafe) = Result.catching[IOException](scala.Console.err.print(s))
def println(s: String)(using AllowUnsafe) = Result.catching[IOException](scala.Console.out.println(s))
def printlnErr(s: String)(using AllowUnsafe) = Result.catching[IOException](scala.Console.err.println(s))
def flush()(using AllowUnsafe) = Result.catching[IOException](scala.Console.flush())
.flatMap(_.toResult(Result.fail(new EOFException("Consoles.readLine failed."))))
def print(s: String)(using AllowUnsafe) = Result.catching[IOException](scala.Console.out.print(s))
def printErr(s: String)(using AllowUnsafe) = Result.catching[IOException](scala.Console.err.print(s))
def printLine(s: String)(using AllowUnsafe) = Result.catching[IOException](scala.Console.out.println(s))
def printLineErr(s: String)(using AllowUnsafe) = Result.catching[IOException](scala.Console.err.println(s))
def flush()(using AllowUnsafe) = Result.catching[IOException](scala.Console.flush())
)

private val local = Local.init(live)
Expand Down Expand Up @@ -122,8 +122,8 @@ object Console:
val it = lines.iterator
val proxy =
new Proxy(console.unsafe):
override def readln()(using AllowUnsafe) =
if !it.hasNext then Result.fail(new EOFException("Consoles.readln failed."))
override def readLine()(using AllowUnsafe) =
if !it.hasNext then Result.fail(new EOFException("Consoles.readLine failed."))
else Result.success(it.next())
let(Console(proxy))(v)
}
Expand Down Expand Up @@ -156,10 +156,10 @@ object Console:
val stdErr = new StringBuffer
val proxy =
new Proxy(console.unsafe):
override def print(s: String)(using AllowUnsafe) = Result.success(stdOut.append(s)).unit
override def printErr(s: String)(using AllowUnsafe) = Result.success(stdErr.append(s)).unit
override def println(s: String)(using AllowUnsafe) = Result.success(stdOut.append(s + "\n")).unit
override def printlnErr(s: String)(using AllowUnsafe) = Result.success(stdErr.append(s + "\n")).unit
override def print(s: String)(using AllowUnsafe) = Result.success(stdOut.append(s)).unit
override def printErr(s: String)(using AllowUnsafe) = Result.success(stdErr.append(s)).unit
override def printLine(s: String)(using AllowUnsafe) = Result.success(stdOut.append(s + "\n")).unit
override def printLineErr(s: String)(using AllowUnsafe) = Result.success(stdErr.append(s + "\n")).unit
let(Console(proxy))(v)
.map(r => IO((Out(stdOut.toString(), stdErr.toString()), r)))
}
Expand All @@ -170,8 +170,8 @@ object Console:
* @return
* A String representing the line read from the console.
*/
def readln(using Frame): String < (IO & Abort[IOException]) =
local.use(_.readln)
def readLine(using Frame): String < (IO & Abort[IOException]) =
local.use(_.readLine)

private def toString(v: Any)(using Frame): String =
v match
Expand Down Expand Up @@ -201,35 +201,35 @@ object Console:
* @param v
* The value to print.
*/
def println[A](v: A)(using Frame): Unit < (IO & Abort[IOException]) =
def printLine[A](v: A)(using Frame): Unit < (IO & Abort[IOException]) =
local.use(_.println(toString(v)))

/** Prints a value to the console's error stream followed by a newline.
*
* @param v
* The value to print to the error stream.
*/
def printlnErr[A](v: A)(using Frame): Unit < (IO & Abort[IOException]) =
local.use(_.printlnErr(toString(v)))
def printLineErr[A](v: A)(using Frame): Unit < (IO & Abort[IOException]) =
local.use(_.printLineErr(toString(v)))

/** WARNING: Low-level API meant for integrations, libraries, and performance-sensitive code. See AllowUnsafe for more details. */
abstract class Unsafe:
def readln()(using AllowUnsafe): Result[IOException, String]
def readLine()(using AllowUnsafe): Result[IOException, String]
def print(s: String)(using AllowUnsafe): Result[IOException, Unit]
def printErr(s: String)(using AllowUnsafe): Result[IOException, Unit]
def println(s: String)(using AllowUnsafe): Result[IOException, Unit]
def printlnErr(s: String)(using AllowUnsafe): Result[IOException, Unit]
def printLine(s: String)(using AllowUnsafe): Result[IOException, Unit]
def printLineErr(s: String)(using AllowUnsafe): Result[IOException, Unit]
def flush()(using AllowUnsafe): Result[IOException, Unit]
def safe: Console = Console(this)
end Unsafe

private class Proxy(underlying: Unsafe) extends Unsafe:
def readln()(using AllowUnsafe) = underlying.readln()
def print(s: String)(using AllowUnsafe) = underlying.print(s)
def printErr(s: String)(using AllowUnsafe) = underlying.printErr(s)
def println(s: String)(using AllowUnsafe) = underlying.println(s)
def printlnErr(s: String)(using AllowUnsafe) = underlying.printlnErr(s)
def flush()(using AllowUnsafe) = underlying.flush()
def readLine()(using AllowUnsafe) = underlying.readLine()
def print(s: String)(using AllowUnsafe) = underlying.print(s)
def printErr(s: String)(using AllowUnsafe) = underlying.printErr(s)
def printLine(s: String)(using AllowUnsafe) = underlying.printLine(s)
def printLineErr(s: String)(using AllowUnsafe) = underlying.printLineErr(s)
def flush()(using AllowUnsafe) = underlying.flush()
end Proxy

end Console
22 changes: 11 additions & 11 deletions kyo-core/shared/src/test/scala/kyo/ConsoleTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ConsoleTest extends Test:

"readln" in run {
Console.withIn(List("readln")) {
Console.readln.map { result =>
Console.readLine.map { result =>
assert(result == "readln")
}
}
Expand All @@ -24,12 +24,12 @@ class ConsoleTest extends Test:
}
}
"println" in run {
Console.withOut(Console.println("print")).map { (out, _) =>
Console.withOut(Console.printLine("print")).map { (out, _) =>
assert(out.stdOut == "print\n")
}
}
"printlnErr" in run {
Console.withOut(Console.printlnErr("print")).map { (out, _) =>
Console.withOut(Console.printLineErr("print")).map { (out, _) =>
assert(out.stdErr == "print\n")
}
}
Expand All @@ -47,9 +47,9 @@ class ConsoleTest extends Test:
IO.Unsafe.run {
for
r1 <- Abort.run(Console.print("test"))
r2 <- Abort.run(Console.println(" message"))
r2 <- Abort.run(Console.printLine(" message"))
r3 <- Abort.run(Console.printErr("error"))
r4 <- Abort.run(Console.printlnErr(" message"))
r4 <- Abort.run(Console.printLineErr(" message"))
yield
assert(r1.isSuccess && r2.isSuccess && r3.isSuccess && r4.isSuccess)
assert(output.toString == "test message\n")
Expand All @@ -64,7 +64,7 @@ class ConsoleTest extends Test:

"should read line correctly" in {
val testUnsafe = new TestUnsafeConsole("test input")
assert(testUnsafe.readln() == Result.success("test input"))
assert(testUnsafe.readLine() == Result.success("test input"))
}

"should print correctly" in {
Expand All @@ -81,13 +81,13 @@ class ConsoleTest extends Test:

"should println correctly" in {
val testUnsafe = new TestUnsafeConsole()
testUnsafe.println("test line")
testUnsafe.printLine("test line")
assert(testUnsafe.printlns.head == "test line")
}

"should println error correctly" in {
val testUnsafe = new TestUnsafeConsole()
testUnsafe.printlnErr("test error line")
testUnsafe.printLineErr("test error line")
assert(testUnsafe.printlnErrs.head == "test error line")
}

Expand All @@ -105,18 +105,18 @@ class ConsoleTest extends Test:
var printlns = List.empty[String]
var printlnErrs = List.empty[String]

def readln()(using AllowUnsafe) =
def readLine()(using AllowUnsafe) =
Result.success(readlnInput)
def print(s: String)(using AllowUnsafe) =
prints = s :: prints
Result.unit
def printErr(s: String)(using AllowUnsafe) =
printErrs = s :: printErrs
Result.unit
def println(s: String)(using AllowUnsafe) =
def printLine(s: String)(using AllowUnsafe) =
printlns = s :: printlns
Result.unit
def printlnErr(s: String)(using AllowUnsafe) =
def printLineErr(s: String)(using AllowUnsafe) =
printlnErrs = s :: printlnErrs
Result.unit
def flush()(using AllowUnsafe) = Result.unit
Expand Down
4 changes: 2 additions & 2 deletions kyo-core/shared/src/test/scala/kyo/KyoAppTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class KyoAppTest extends Test:
val app = new KyoApp:
run {
for
_ <- Console.println(s"Starting with args [${args.mkString(", ")}]")
_ <- Console.printLine(s"Starting with args [${args.mkString(", ")}]")
yield "Exit!"
}

Expand All @@ -32,7 +32,7 @@ class KyoAppTest extends Test:
for
_ <- Clock.repeatAtInterval(1.second, 1.second)(())
i <- Random.nextInt
_ <- Console.println(s"$i")
_ <- Console.printLine(s"$i")
_ <- Clock.now
_ <- Resource.ensure(())
_ <- Async.run(())
Expand Down
Loading

0 comments on commit 2c24337

Please sign in to comment.