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

data: Rename Maybe.Defined/Empty to Present/Absent and export them to the kyo package #734

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix readme
  • Loading branch information
fwbrasil committed Oct 14, 2024
commit d2fcbd90a56996d451c97f8aa38d3c6f5dea77cc
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ In Kyo, computations are expressed via the infix type `<`, known as "Pending". I
```scala
import kyo.*

// 'Int' pending 'Abort[Maybe.Empty]'
Int < Abort[Maybe.Empty]
// 'Int' pending 'Abort[Absent]'
// 'Absent' is Kyo's equivalent of 'None' via the 'Maybe' type
Int < Abort[Absent]

// 'String' pending 'Abort[Maybe.Empty]' and 'IO'
String < (Abort[Maybe.Empty] & IO)
// 'String' pending 'Abort[Absent]' and 'IO'
String < (Abort[Absent] & IO)
```

> Note: The naming convention for effect types is the plural form of the functionalities they manage.
Expand Down Expand Up @@ -2616,8 +2617,8 @@ import kyo._
// Create a 'Maybe' value
val a: Maybe[Int] = Maybe(42)

// 'Maybe.empty' represents the absence of a value
val b: Maybe[Int] = Maybe.empty[Int]
// 'Absent' represents the absence of a value
val b: Maybe[Int] = Absent

// 'Maybe.when' conditionally creates a 'Maybe' value
val c: Maybe[Int] = Maybe.when(true)(42)
Expand Down Expand Up @@ -2680,6 +2681,12 @@ val s: Maybe[Int] = for {
// Nesting 'Maybe' values
val nested: Maybe[Maybe[Int]] = Maybe(Maybe(42))
val flattened: Maybe[Int] = nested.flatten

// Pattern matching with 'Present' and 'Absent'
val result: String =
flattened match
case Present(value) => s"Value: $value"
case Absent => "No value"
```

### Duration: Time Representation
Expand Down Expand Up @@ -3206,7 +3213,7 @@ import kyo.*

// An example computation with
// nested effects
val a: Int < IO < Abort[Maybe.Empty] =
val a: Int < IO < Abort[Absent] =
Abort.get(Some(IO(1)))

// Can't handle a effects of a
Expand All @@ -3226,15 +3233,15 @@ Kyo performs checks at compilation time to ensure that nested effects are not us
```scala
import kyo.*

// def test[T](v: T < Abort[Maybe.Empty]) =
// def test[T](v: T < Abort[Absent]) =
// Abort.run(v)
// Compilation failure:
// Method doesn't accept nested Kyo computations.
// Cannot prove 'T' isn't nested. This error can be reported an unsupported pending effect is passed to a method. If that's not the case, provide an implicit evidence 'kyo.Flat[T]'.

// It's possible to provide an implicit
// evidence of `Flat` to resolve
def test[T](v: T < Abort[Maybe.Empty])(using Flat[T]) =
def test[T](v: T < Abort[Absent])(using Flat[T]) =
Abort.run(v)
```

Expand Down Expand Up @@ -3310,15 +3317,15 @@ IO.Unsafe.run { // Handles IO

### Failure conversions

One notable departure from the ZIO API worth calling out is a set of combinators for converting between failure effects. Whereas ZIO has a single channel for describing errors, Kyo has different effect types that can describe failure in the basic sense of "short-circuiting": `Abort` and `Choice` (an empty `Seq` being equivalent to a short-circuit). `Abort[Maybe.Empty]` can also be used like `Choice` to model short-circuiting an empty result. It's useful to be able to move between these effects easily, so `kyo-combinators` provides a number of extension methods, usually in the form of `def effect1ToEffect2`.
One notable departure from the ZIO API worth calling out is a set of combinators for converting between failure effects. Whereas ZIO has a single channel for describing errors, Kyo has different effect types that can describe failure in the basic sense of "short-circuiting": `Abort` and `Choice` (an empty `Seq` being equivalent to a short-circuit). `Abort[Absent]` can also be used like `Choice` to model short-circuiting an empty result. It's useful to be able to move between these effects easily, so `kyo-combinators` provides a number of extension methods, usually in the form of `def effect1ToEffect2`.

Some examples:

```scala
val abortEffect: Int < Abort[String] = ???

// Converts failures to empty failure
val maybeEffect: Int < Abort[Maybe.Empty] = abortEffect.abortToEmpty
val maybeEffect: Int < Abort[Absent] = abortEffect.abortToEmpty

// Converts empty failure to a single "choice" (or Seq)
val choiceEffect: Int < Choice = maybeEffect.emptyAbortToChoice
Expand Down
Loading