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

Move code Arrow-Core #2403

Merged
merged 7 commits into from
May 21, 2021
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
Next Next commit
Move code Arrow-Core
nomisRev committed May 20, 2021
commit 27e5e4df45f1e7a997a71a91c3079d55b2499d56
5 changes: 2 additions & 3 deletions arrow-libs/core/arrow-annotations/build.gradle
Original file line number Diff line number Diff line change
@@ -10,10 +10,9 @@ apply from: "$ANIMALSNIFFER"

kotlin {
sourceSets {
jvmMain {
commonMain {
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$KOTLIN_VERSION"
api "io.kindedj:kindedj:$KINDEDJ_VERSION"
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-common:$KOTLIN_VERSION"
}
}
}
5 changes: 5 additions & 0 deletions arrow-libs/core/arrow-continuations/build.gradle
Original file line number Diff line number Diff line change
@@ -12,6 +12,11 @@ apply plugin: 'kotlinx-atomicfu'

kotlin {
sourceSets {
commonMain {
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-common:$KOTLIN_VERSION"
}
}
jvmMain {
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$KOTLIN_VERSION"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package arrow.continuations.generic

/**
* A [Throwable] class intended for control flow.
* Instance of [ControlThrowable] should **not** be caught,
* and `arrow.core.NonFatal` does not catch this [Throwable].
* Thus by extension `Either.catch` and `Validated.catch` also don't catch [ControlThrowable].
*/
expect open class ControlThrowable() : Throwable
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ internal open class DelimContScope<R>(private val f: suspend RestrictedScope<R>.
override suspend fun <A> shift(f: suspend RestrictedScope<R>.(DelimitedContinuation<A, R>) -> R): A =
suspendCoroutineUninterceptedOrReturn { continueMain ->
val delCont = SingleShotCont(continueMain, shiftFnContinuations)
assert(nextShift == null)
require(nextShift == null)
nextShift = suspend { this.f(delCont) }
COROUTINE_SUSPENDED
}
@@ -75,7 +75,7 @@ internal open class DelimContScope<R>(private val f: suspend RestrictedScope<R>.
*/
suspend fun <A, B> shiftCPS(f: suspend (DelimitedContinuation<A, B>) -> R, c: suspend DelimitedScope<B>.(A) -> B): Nothing =
suspendCoroutineUninterceptedOrReturn {
assert(nextShift == null)
require(nextShift == null)
nextShift = suspend { f(CPSCont(c)) }
COROUTINE_SUSPENDED
}
@@ -116,7 +116,7 @@ internal open class DelimContScope<R>(private val f: suspend RestrictedScope<R>.
// we can return directly if we never suspended/called shift
else return@invoke it as R
}
assert(resultVar !== EMPTY_VALUE)
require(resultVar !== EMPTY_VALUE)
// We need to finish the partially evaluated shift blocks by passing them our result.
// This will update the result via the continuations that now finish up
for (c in shiftFnContinuations.asReversed()) c.resume(resultVar as R)
Original file line number Diff line number Diff line change
@@ -77,14 +77,14 @@ internal open class MultiShotDelimContScope<R>(val f: suspend RestrictedScope<R>
suspendCoroutineUninterceptedOrReturn { continueMain ->
val c = MultiShotCont(continueMain, f, stack, shiftFnContinuations)
val s: suspend RestrictedScope<R>.() -> R = { this.func(c) }
assert(nextShift.compareAndSet(null, s))
require(nextShift.compareAndSet(null, s))
COROUTINE_SUSPENDED
}

suspend fun <A, B> shiftCPS(func: suspend DelimitedScope<R>.(DelimitedContinuation<A, B>) -> R, c: suspend DelimitedScope<B>.(A) -> B): Nothing =
suspendCoroutine {
val s: suspend DelimitedScope<R>.() -> R = { func(CPSCont(c)) }
assert(nextShift.compareAndSet(null, s))
require(nextShift.compareAndSet(null, s))
}

// This assumes RestrictSuspension or at least assumes the user to never reference the parent scope in f.
@@ -115,7 +115,7 @@ internal open class MultiShotDelimContScope<R>(val f: suspend RestrictedScope<R>
}
} else return@invoke it as R
}
assert(resultVar.value != null)
require(resultVar.value != null)
for (c in shiftFnContinuations.asReversed()) c.resume(resultVar.value!!)
return resultVar.value!!
}
Original file line number Diff line number Diff line change
@@ -2,5 +2,6 @@ package arrow.continuations.generic

/** Represents a unique identifier using object equality. */
internal class Token {
override fun toString(): String = "Token(${Integer.toHexString(hashCode())})"
@ExperimentalUnsignedTypes
override fun toString(): String = "Token(${hashCode().toUInt().toString(16)})"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

package arrow.continuations.generic

/**
* A [Throwable] class intended for control flow.
* Instance of [ControlThrowable] should **not** be caught,
* Instance of [ControlThrowable.kt] should **not** be caught,
* and `arrow.core.NonFatal` does not catch this [Throwable].
* Thus by extension `Either.catch` and `Validated.catch` also don't catch [ControlThrowable].
* Thus by extension `Either.catch` and `Validated.catch` also don't catch [ControlThrowable.kt].
*/
open class ControlThrowable : Throwable() {
actual open class ControlThrowable : Throwable() {
override fun fillInStackTrace(): Throwable = this
}
30 changes: 8 additions & 22 deletions arrow-libs/core/arrow-core-retrofit/build.gradle
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.jvm"
id "org.jlleitschuh.gradle.ktlint"
}

apply from: "$SUB_PROJECT_MPP"
apply from: "$SUB_PROJECT"
apply from: "$TEST_COVERAGE"
apply from: "$DOC_CREATION"
apply from: "$PUBLICATION_MPP"
apply from: "$ANIMALSNIFFER"

kotlin {
sourceSets {
jvmMain {
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$KOTLIN_VERSION"
compileOnly project(":arrow-core")
compileOnly "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION"
}
}

jvmTest {
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-reflect:$KOTLIN_VERSION"
runtimeOnly "org.junit.vintage:junit-vintage-engine:$JUNIT_VINTAGE_VERSION"
implementation project(":arrow-core-test")
implementation "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION"
implementation "com.squareup.okhttp3:mockwebserver:$MOCKWEBSERVER_VERSION"
}
}
}
dependencies {
api project(":arrow-core")
api project(":arrow-continuations")
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$KOTLINX_COROUTINES_VERSION"
api "io.kotlintest:kotlintest-runner-junit5:$KOTLIN_TEST_VERSION", excludeArrow
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:$JUNIT_VINTAGE_VERSION"
}
9 changes: 7 additions & 2 deletions arrow-libs/core/arrow-core/build.gradle
Original file line number Diff line number Diff line change
@@ -13,12 +13,17 @@ apply from: "$ANIMALSNIFFER"

kotlin {
sourceSets {
commonMain {
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-common:$KOTLIN_VERSION"
api project(":arrow-continuations")
api project(":arrow-annotations")
}
}
jvmMain {
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$KOTLIN_VERSION"
compileOnly "org.codehaus.mojo:animal-sniffer-annotations:1.19"
api project(":arrow-continuations")
api project(":arrow-annotations")
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package arrow.core

@PublishedApi
internal expect object ArrowCoreInternalException : RuntimeException
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@ import arrow.core.Either.Left
import arrow.core.Either.Right
import arrow.typeclasses.Monoid
import arrow.typeclasses.Semigroup
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic

/**
*
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package arrow.core

import arrow.typeclasses.Monoid
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
import kotlin.jvm.JvmStatic

/**
* Eval is a monad which controls evaluation of a value or a computation that produces a value.
@@ -233,7 +233,7 @@ sealed class Eval<out A> {
is FlatMap<A> -> object : FlatMap<B>() {
override fun <S> start(): Eval<S> = (this@Eval).start()

@IgnoreJRERequirement
// @IgnoreJRERequirement
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this annotation is not needed anymore. AnimalSniffer seems to pass on this code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'm going to review AnimalSniffer configuration because maybe it must be updated with the last change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, if we need it back we can turn it into a expect/actual annotation where native and JS have no-op annotations.

override fun <S> run(s: S): Eval<B> =
object : FlatMap<B>() {
override fun <S1> start(): Eval<S1> = (this@Eval).run(s) as Eval<S1>
@@ -381,15 +381,35 @@ fun <A, B, C, D> Eval<A>.zip(
c: Eval<C>,
map: (A, B, C) -> D
): Eval<D> =
zip(b, c, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, _, _, _, _, _, _, _ -> map(aa, bb, cc) }
zip(
b,
c,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit
) { aa, bb, cc, _, _, _, _, _, _, _ -> map(aa, bb, cc) }

fun <A, B, C, D, E> Eval<A>.zip(
b: Eval<B>,
c: Eval<C>,
d: Eval<D>,
map: (A, B, C, D) -> E
): Eval<E> =
zip(b, c, d, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, _, _, _, _, _, _ -> map(aa, bb, cc, dd) }
zip(
b,
c,
d,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit,
Eval.Now.unit
) { aa, bb, cc, dd, _, _, _, _, _, _ -> map(aa, bb, cc, dd) }

fun <A, B, C, D, E, F> Eval<A>.zip(
b: Eval<B>,
@@ -398,7 +418,15 @@ fun <A, B, C, D, E, F> Eval<A>.zip(
e: Eval<E>,
map: (A, B, C, D, E) -> F
): Eval<F> =
zip(b, c, d, e, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, ee, _, _, _, _, _ -> map(aa, bb, cc, dd, ee) }
zip(b, c, d, e, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, ee, _, _, _, _, _ ->
map(
aa,
bb,
cc,
dd,
ee
)
}

fun <A, B, C, D, E, F, G> Eval<A>.zip(
b: Eval<B>,
@@ -408,7 +436,16 @@ fun <A, B, C, D, E, F, G> Eval<A>.zip(
f: Eval<F>,
map: (A, B, C, D, E, F) -> G
): Eval<G> =
zip(b, c, d, e, f, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, ee, ff, _, _, _, _ -> map(aa, bb, cc, dd, ee, ff) }
zip(b, c, d, e, f, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, ee, ff, _, _, _, _ ->
map(
aa,
bb,
cc,
dd,
ee,
ff
)
}

fun <A, B, C, D, E, F, G, H> Eval<A>.zip(
b: Eval<B>,
@@ -419,7 +456,17 @@ fun <A, B, C, D, E, F, G, H> Eval<A>.zip(
g: Eval<G>,
map: (A, B, C, D, E, F, G) -> H
): Eval<H> =
zip(b, c, d, e, f, g, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, ee, ff, gg, _, _, _ -> map(aa, bb, cc, dd, ee, ff, gg) }
zip(b, c, d, e, f, g, Eval.Now.unit, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, ee, ff, gg, _, _, _ ->
map(
aa,
bb,
cc,
dd,
ee,
ff,
gg
)
}

fun <A, B, C, D, E, F, G, H, I> Eval<A>.zip(
b: Eval<B>,
@@ -431,7 +478,18 @@ fun <A, B, C, D, E, F, G, H, I> Eval<A>.zip(
h: Eval<H>,
map: (A, B, C, D, E, F, G, H) -> I
): Eval<I> =
zip(b, c, d, e, f, g, h, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, ee, ff, gg, hh, _, _ -> map(aa, bb, cc, dd, ee, ff, gg, hh) }
zip(b, c, d, e, f, g, h, Eval.Now.unit, Eval.Now.unit) { aa, bb, cc, dd, ee, ff, gg, hh, _, _ ->
map(
aa,
bb,
cc,
dd,
ee,
ff,
gg,
hh
)
}

fun <A, B, C, D, E, F, G, H, I, J> Eval<A>.zip(
b: Eval<B>,
@@ -444,7 +502,19 @@ fun <A, B, C, D, E, F, G, H, I, J> Eval<A>.zip(
i: Eval<I>,
map: (A, B, C, D, E, F, G, H, I) -> J
): Eval<J> =
zip(b, c, d, e, f, g, h, i, Eval.Now.unit) { aa, bb, cc, dd, ee, ff, gg, hh, ii, _ -> map(aa, bb, cc, dd, ee, ff, gg, hh, ii) }
zip(b, c, d, e, f, g, h, i, Eval.Now.unit) { aa, bb, cc, dd, ee, ff, gg, hh, ii, _ ->
map(
aa,
bb,
cc,
dd,
ee,
ff,
gg,
hh,
ii
)
}

fun <A, B, C, D, E, F, G, H, I, J, K> Eval<A>.zip(
b: Eval<B>,
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import arrow.core.Ior.Left
import arrow.core.Ior.Right
import arrow.typeclasses.Monoid
import arrow.typeclasses.Semigroup
import kotlin.jvm.JvmStatic

typealias IorNel<A, B> = Ior<Nel<A>, B>

Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package arrow.core
import arrow.core.Either.Left
import arrow.core.Either.Right
import arrow.typeclasses.Semigroup
import kotlin.jvm.JvmStatic

typealias Nel<A> = NonEmptyList<A>

@@ -201,13 +202,12 @@ class NonEmptyList<out A>(

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false

other as NonEmptyList<*>

if (all != other.all) return false

return true
other?.let {
if (it::class != this::class) return false
(other as NonEmptyList<*>)
if (all != other.all) return false
return true
} ?: return false
}

override fun hashCode(): Int =
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package arrow.core

import kotlin.coroutines.cancellation.CancellationException

/**
* Extractor of non-fatal Throwable. Will not match fatal errors like `VirtualMachineError`
* (for example, `OutOfMemoryError` and `StackOverflowError`, subclasses of `VirtualMachineError`), `ThreadDeath`,
* `LinkageError`, `InterruptedException`.
* This will also not match [CancellationException] since that's a fatal exception in Kotlin for cancellation purposes.
*
* Checks whether the passed [t] Throwable is NonFatal.
*
* @param t the Throwable to check
* @return true if the provided `Throwable` is to be considered non-fatal, or false if it is to be considered fatal
*
* ```kotlin:ank:playground
* import arrow.*
* import arrow.core.*
*
* fun unsafeFunction(i: Int): String =
* when (i) {
* 1 -> throw IllegalArgumentException("Non-Fatal")
* 2 -> throw OutOfMemoryError("Fatal")
* else -> "Hello"
* }
*
* fun main(args: Array<String>) {
* val nonFatal: Either<Throwable, String> =
* //sampleStart
* try {
* Right(unsafeFunction(1))
* } catch (t: Throwable) {
* if (NonFatal(t)) {
* Left(t)
* } else {
* throw t
* }
* }
* //sampleEnd
* println(nonFatal)
* }
* ```
*
*/
expect fun NonFatal(t: Throwable): Boolean
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

package arrow.core

import kotlin.jvm.JvmStatic

object Nullable {

@JvmStatic
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@ package arrow.core
import arrow.core.Either.Right
import arrow.typeclasses.Monoid
import arrow.typeclasses.Semigroup
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic

/**
*
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
package arrow.core

import arrow.typeclasses.Semigroup
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

operator fun <A : Comparable<A>, B : Comparable<B>> Pair<A, B>.compareTo(other: Pair<A, B>): Int {
val first = first.compareTo(other.first)
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

operator fun <A : Comparable<A>, B : Comparable<B>, C : Comparable<C>> Triple<A, B, C>.compareTo(other: Triple<A, B, C>): Int {
val first = first.compareTo(other.first)
return if (first == 0) {
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

data class Tuple10<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J>(
val first: A,
val second: B,
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

data class Tuple4<out A, out B, out C, out D>(
val first: A,
val second: B,
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

data class Tuple5<out A, out B, out C, out D, out E>(
val first: A,
val second: B,
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

data class Tuple6<out A, out B, out C, out D, out E, out F>(
val first: A,
val second: B,
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

data class Tuple7<out A, out B, out C, out D, out E, out F, out G>(
val first: A,
val second: B,
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

data class Tuple8<out A, out B, out C, out D, out E, out F, out G, out H>(
val first: A,
val second: B,
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

data class Tuple9<out A, out B, out C, out D, out E, out F, out G, out H, out I>(
val first: A,
val second: B,
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@

package arrow.core

import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

data class Tuple11<out A, out B, out C, out D, out E, out F, out G, out H, out I, out J, out K>(
val first: A,
val second: B,
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ import arrow.typeclasses.Monoid
import arrow.typeclasses.Semigroup
import arrow.core.Either.Left
import arrow.core.Either.Right
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic

typealias ValidatedNel<E, A> = Validated<Nel<E>, A>
typealias Valid<A> = Validated.Valid<A>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@file:JvmName("Composition")
package arrow.core

import kotlin.jvm.JvmName

infix fun <P1, P2, IP, R> ((P1, P2) -> IP).andThen(f: (IP) -> R): (P1, P2) -> R =
AndThen2(this).andThen(f)

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@file:JvmName("Currying")
package arrow.core

import kotlin.jvm.JvmName

fun <P1, P2, R> ((P1, P2) -> R).curried(): (P1) -> (P2) -> R =
{ p1: P1 -> { p2: P2 -> this(p1, p2) } }

Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package arrow.core

import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.loop
import kotlin.jvm.JvmName

/**
* Memoizes the given **pure** function so that invocations with the same arguments will only execute the function once.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package arrow.core

/**
* Returns the Throwable if NonFatal and throws it otherwise.
*
* @throws Throwable the Throwable `this` if Fatal
* @return the Throwable `this` if NonFatal
*
* ```kotlin:ank:playground
* import arrow.*
* import arrow.core.*
*
* fun unsafeFunction(i: Int): String =
* when (i) {
* 1 -> throw IllegalArgumentException("Non-Fatal")
* 2 -> throw OutOfMemoryError("Fatal")
* else -> "Hello"
* }
*
* fun main(args: Array<String>) {
* val nonFatal: Either<Throwable, String> =
* //sampleStart
* try {
* Right(unsafeFunction(1))
* } catch (t: Throwable) {
* Left(t.nonFatalOrThrow())
* }
* //sampleEnd
* println(nonFatal)
* }
* ```
*
*/
// https://youtrack.jetbrains.com/issue/KT-36036
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link to issue on Kotlin tracker.
This extension cannot live in NonFatal.kt anymore since it conflicts with the NonFatalKt file generated by jvmMain which has the expect implementation for NonFatal.kt.

fun Throwable.nonFatalOrThrow(): Throwable =
if (NonFatal(this)) this else throw this
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
@file:JvmName("Partials")
package arrow.core

import kotlin.jvm.JvmName

fun <P1, R> ((P1) -> R).partially1(p1: P1): () -> R =
{ this(p1) }

Original file line number Diff line number Diff line change
@@ -4,14 +4,6 @@ import arrow.typeclasses.Semigroup

inline fun <A> identity(a: A): A = a

@PublishedApi
internal object ArrowCoreInternalException : RuntimeException(
"Arrow-Core internal error. Please let us know and create a ticket at https://github.com/arrow-kt/arrow-core/issues/new/choose",
null
) {
override fun fillInStackTrace(): Throwable = this
}

const val TailRecMDeprecation: String =
"tailRecM is deprecated together with the Kind type classes since it's meant for writing kind-based polymorphic stack-safe programs."

Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@ import arrow.core.combineAll
import arrow.core.compose
import arrow.core.flatten
import arrow.core.identity
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic
import kotlin.collections.plus as _plus

interface Monoid<A> : Semigroup<A> {
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@ import arrow.core.Option
import arrow.core.Validated
import arrow.core.combine
import arrow.core.compose
import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic

interface Semigroup<A> {
/**
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package arrow.typeclasses

import kotlin.jvm.JvmName
import kotlin.jvm.JvmStatic

/**
* The [Semiring] type class for a given type `A` combines both a commutative additive [Monoid] and a multiplicative [Monoid].
* It requires the multiplicative [Monoid] to distribute over the additive one. The operations of the multiplicative [Monoid] have been renamed to
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package arrow.core

@PublishedApi
internal actual object ArrowCoreInternalException : RuntimeException(
"Arrow-Core internal error. Please let us know and create a ticket at https://github.com/arrow-kt/arrow-core/issues/new/choose",
null
) {
override fun fillInStackTrace(): Throwable = this
}
Original file line number Diff line number Diff line change
@@ -1,84 +1,12 @@
@file:JvmMultifileClass
@file:JvmName("NonFatalKt")
package arrow.core

import arrow.continuations.generic.ControlThrowable
import kotlin.coroutines.cancellation.CancellationException

/**
* Extractor of non-fatal Throwable. Will not match fatal errors like `VirtualMachineError`
* (for example, `OutOfMemoryError` and `StackOverflowError`, subclasses of `VirtualMachineError`), `ThreadDeath`,
* `LinkageError`, `InterruptedException`.
* This will also not match [CancellationException] since that's a fatal exception in Kotlin for cancellation purposes.
*
* Checks whether the passed [t] Throwable is NonFatal.
*
* @param t the Throwable to check
* @return true if the provided `Throwable` is to be considered non-fatal, or false if it is to be considered fatal
*
* ```kotlin:ank:playground
* import arrow.*
* import arrow.core.*
*
* fun unsafeFunction(i: Int): String =
* when (i) {
* 1 -> throw IllegalArgumentException("Non-Fatal")
* 2 -> throw OutOfMemoryError("Fatal")
* else -> "Hello"
* }
*
* fun main(args: Array<String>) {
* val nonFatal: Either<Throwable, String> =
* //sampleStart
* try {
* Right(unsafeFunction(1))
* } catch (t: Throwable) {
* if (NonFatal(t)) {
* Left(t)
* } else {
* throw t
* }
* }
* //sampleEnd
* println(nonFatal)
* }
* ```
*
*/
fun NonFatal(t: Throwable): Boolean =
actual fun NonFatal(t: Throwable): Boolean =
when (t) {
is VirtualMachineError, is ThreadDeath, is InterruptedException, is LinkageError, is ControlThrowable, is CancellationException -> false
else -> true
}

/**
* Returns the Throwable if NonFatal and throws it otherwise.
*
* @throws Throwable the Throwable `this` if Fatal
* @return the Throwable `this` if NonFatal
*
* ```kotlin:ank:playground
* import arrow.*
* import arrow.core.*
*
* fun unsafeFunction(i: Int): String =
* when (i) {
* 1 -> throw IllegalArgumentException("Non-Fatal")
* 2 -> throw OutOfMemoryError("Fatal")
* else -> "Hello"
* }
*
* fun main(args: Array<String>) {
* val nonFatal: Either<Throwable, String> =
* //sampleStart
* try {
* Right(unsafeFunction(1))
* } catch (t: Throwable) {
* Left(t.nonFatalOrThrow())
* }
* //sampleEnd
* println(nonFatal)
* }
* ```
*
*/
fun Throwable.nonFatalOrThrow(): Throwable =
if (NonFatal(this)) this else throw this