Skip to content

Commit

Permalink
[core] Console improvements (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwbrasil authored Oct 29, 2024
1 parent 7744fd7 commit c9f7e82
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 132 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ The `Choice` effect becomes exceptionally powerful when combined with other effe

```scala
import kyo.*
import java.io.IOException

// Iteratively increment an 'Int' value
// until it reaches 5
Expand All @@ -949,7 +950,7 @@ val d: Int < IO =
)

// Mixing 'Console' with 'Loop'
val e: 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))
Expand Down Expand Up @@ -996,9 +997,10 @@ In addition to the transform methods, Loop also provides indexed variants that p

```scala
import kyo.*
import java.io.IOException

// Print a message every 3 iterations
val a: Int < IO =
val a: Int < (IO & Abort[IOException]) =
Loop.indexed(1)((idx, i) =>
if idx < 10 then
if idx % 3 == 0 then
Expand Down Expand Up @@ -1174,6 +1176,7 @@ The Stream effect provides a powerful mechanism for processing sequences of data

```scala
import kyo.*
import java.io.IOException

// Create a stream from a sequence
val a: Stream[Int, Any] =
Expand Down Expand Up @@ -1216,7 +1219,7 @@ val j: Int < Any =
a.runFold(0)(_ + _)

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

Expand Down Expand Up @@ -1468,29 +1471,30 @@ The `CheckFailed` exception class, which is used to represent failed checks, inc

```scala
import kyo.*
import java.io.IOException

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

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

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

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

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

// Explicitly specifying the 'Console' implementation
val f: Unit < IO =
val f: Unit < (IO & Abort[IOException]) =
Console.let(Console.live)(e)
```

Expand Down Expand Up @@ -1720,6 +1724,7 @@ All methods that perform side effects are suspended using the `IO` effect, ensur

```scala
import kyo.*
import java.io.IOException

val path: Path = Path("tmp", "file.txt")

Expand All @@ -1728,15 +1733,15 @@ val lines: Stream[String, Resource & IO] =
path.readLinesStream()

// Process the stream
val result: Unit < (Resource & Console & Async) =
val result: Unit < (Resource & Console & Async & Abort[IOException]) =
lines.map(line => Console.println(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) =
val processedTree: Unit < (Console & Async & Abort[IOException]) =
tree.map(file => file.read.map(content => Console.println(s"File: ${file}, Content: $content"))).runDiscard
```

Expand Down Expand Up @@ -3262,6 +3267,7 @@ Generally speaking, the names of `kyo-combinators` methods are the same as the c
```scala 3
import kyo.*
import scala.concurrent.duration.*
import java.io.IOException

trait HelloService:
def sayHelloTo(saluee: String): Unit < (IO & Abort[Throwable])
Expand All @@ -3275,7 +3281,7 @@ object HelloService:
end Live
end HelloService

val keepTicking: Nothing < (Console & Async) =
val keepTicking: Nothing < (Console & Async & Abort[IOException]) =
(Console.print(".") *> Kyo.sleep(1.second)).forever

val effect: Unit < (Console & Async & Resource & Abort[Throwable] & Env[NameService]) =
Expand Down
3 changes: 2 additions & 1 deletion kyo-combinators/shared/src/main/scala/kyo/Constructors.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kyo

import java.io.IOException
import kyo.kernel.Boundary
import kyo.kernel.Reducible
import scala.annotation.tailrec
Expand Down Expand Up @@ -87,7 +88,7 @@ extension (kyoObject: Kyo.type)
* @return
* An effect that prints the message to the console
*/
def debugln(message: String)(using Frame): Unit < IO =
def debugln(message: String)(using Frame): Unit < (IO & Abort[IOException]) =
Console.println(message)

/** Creates an effect that fails with Abort[E].
Expand Down
Loading

0 comments on commit c9f7e82

Please sign in to comment.