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

Adds specialised outcomeOf Raise DSL to allow for greater interoperability with Results and Eithers #107

Merged
merged 1 commit into from
Sep 11, 2024

Conversation

cwmyers
Copy link
Collaborator

@cwmyers cwmyers commented Sep 11, 2024

This PR adds a specialised Raise DSL for OutcomeOf<A>.

Result<Option<A>>, ErrorOr<Option<A>> and OutcomeOf<A> are isomorphic, meaning they can be converted between each other without loss of information. Given that is the case we can increase the interoperability between them inside the outcomeOf DSL block.

For example:

val outcomeValue : OutcomeOf<Int> = outcomeOf {
      val a = Present(1).bind()
      val b: Int = Result.success(1.some()).bind()
      val c: Int = Either.Right(2).bind()
      val d: Int = Either.Right(4.some()).bindOption()
      val nullValue: Int? = 1
      val e: Int = ensureNotNull(nullValue)
      val f: Int = nullValue.bindNull()
      val g: Int = Result.success(1).bindResult()
      val result = a + b + c + d + e + f + g
      ensure(result > 9)
      result
}

outcomeValue.shouldBePresent().shouldBe(11)

Given Result<Option<A>> is identical to Outcome<A> binding on that type will now just work. For the Result<A> case however the Result<A> needs to be converted to a OutcomeOf<A> before performing a bind, which can be done with bindResult.

Eithers are already native to the DSL and so need to be handled a little differently. Binding over ErrorOr<Option<A>> will require bindOption to first bind over the either and then bind over the Option inside.

Option and nullable values can also be handled with a bind or bindNull respectively. In each case a None or null value will raise an Absent value.

@cwmyers cwmyers force-pushed the cmyers/2024-09-11-adds-outcomeOr-raise-dsl branch from 87a4797 to c885a7d Compare September 11, 2024 00:34
@cwmyers cwmyers merged commit e24a8c9 into main Sep 11, 2024
2 checks passed
@cwmyers cwmyers deleted the cmyers/2024-09-11-adds-outcomeOr-raise-dsl branch September 11, 2024 00:48
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.

2 participants