Skip to content

Commit

Permalink
Add merge builder for raise (#3061)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>
Co-authored-by: Francisco Diaz <francisco.d@47deg.com>
  • Loading branch information
3 people authored Jun 19, 2023
1 parent f8c50f3 commit 3d7cb6e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -3421,6 +3421,7 @@ public abstract interface annotation class arrow/core/raise/RaiseDSL : java/lang
public final class arrow/core/raise/RaiseKt {
public static final fun _fold (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun _foldOrThrow (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun _merge (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun catch (Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun catch (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function1;
public static final fun catch (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public inline fun <Error, A> either(@BuilderInference block: Raise<Error>.() ->
fold({ block.invoke(this) }, { Either.Left(it) }, { Either.Right(it) })

public inline fun <A> nullable(block: NullableRaise.() -> A): A? =
fold({ block(NullableRaise(this)) }, { null }, ::identity)
merge { block(NullableRaise(this)) }

public inline fun <A> result(block: ResultRaise.() -> A): Result<A> =
fold({ block(ResultRaise(this)) }, Result.Companion::failure, Result.Companion::failure, Result.Companion::success)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@file:OptIn(ExperimentalTypeInference::class)
package arrow.core.raise

import arrow.core.identity
import kotlin.experimental.ExperimentalTypeInference
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
Expand Down Expand Up @@ -672,11 +671,9 @@ public typealias EagerEffect<Error, A> = Raise<Error>.() -> A

public inline fun <Error, A> eagerEffect(@BuilderInference noinline block: Raise<Error>.() -> A): EagerEffect<Error, A> = block

public suspend fun <A> Effect<A, A>.merge(): A = getOrElse(::identity)
public fun <A> EagerEffect<A, A>.merge(): A = getOrElse(::identity)
public suspend fun <A> Effect<A, A>.merge(): A = merge { invoke() }
public fun <A> EagerEffect<A, A>.merge(): A = merge { invoke() }

@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
public suspend fun <A> Effect<Nothing, A>.get(): A = getOrElse(::identity)
public suspend fun <A> Effect<Nothing, A>.get(): A = merge()

@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
public fun <A> EagerEffect<Nothing, A>.get(): A = getOrElse(::identity)
public fun <A> EagerEffect<Nothing, A>.get(): A = merge()
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,27 @@ public inline fun <Error, OtherError, A> Raise<Error>.withError(
}
return recover(block) { raise(transform(it)) }
}

/**
* Execute the [Raise] context function resulting in [A] or any _logical error_ of type [A].
* Does not distinguish between normal results and errors, thus you can consider
* `return` and `raise` to be semantically equivalent inside.
*
* <!--- INCLUDE
* import arrow.core.raise.merge
* import io.kotest.matchers.shouldBe
* import kotlin.random.Random
* -->
* ```kotlin
* fun test() {
* merge { if(Random.nextBoolean()) raise("failed") else "failed" } shouldBe "failed"
* }
* ```
* <!--- KNIT example-raise-dsl-12.kt -->
* <!--- TEST lines.isEmpty() -->
*/
@RaiseDSL
@JvmName("_merge")
public inline fun <A> merge(
@BuilderInference block: Raise<A>.() -> A,
): A = recover(block, ::identity)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file was automatically generated from Raise.kt by Knit tool. Do not edit.
package arrow.core.examples.exampleRaiseDsl12

import arrow.core.raise.merge
import io.kotest.matchers.shouldBe
import kotlin.random.Random

fun test() {
merge { if(Random.nextBoolean()) raise("failed") else "failed" } shouldBe "failed"
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class RaiseKnitTest : StringSpec({
arrow.core.examples.exampleRaiseDsl11.test()
}

"ExampleRaiseDsl12" {
arrow.core.examples.exampleRaiseDsl12.test()
}

}) {
override fun timeout(): Long = 1000
}

0 comments on commit 3d7cb6e

Please sign in to comment.