diff --git a/build.gradle.kts b/build.gradle.kts index d476f0d..a798c3a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -64,6 +64,8 @@ subprojects { } configure { + explicitApi() + jvm { mavenPublication { artifact(javadocJar.get()) diff --git a/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt b/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt index 7a1d47f..0707361 100644 --- a/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt +++ b/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/binding/SuspendableBinding.kt @@ -12,7 +12,7 @@ import kotlin.contracts.contract /** * Suspending variant of [binding][com.github.michaelbull.result.binding]. */ -suspend inline fun binding(crossinline block: suspend SuspendableResultBinding.() -> V): Result { +public suspend inline fun binding(crossinline block: suspend SuspendableResultBinding.() -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } @@ -28,8 +28,8 @@ suspend inline fun binding(crossinline block: suspend SuspendableResultBi internal object BindCancellationException : CancellationException(null) -interface SuspendableResultBinding { - suspend fun Result.bind(): V +public interface SuspendableResultBinding { + public suspend fun Result.bind(): V } @PublishedApi diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt index 68cb6a7..e4881e9 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt @@ -8,7 +8,7 @@ import kotlin.contracts.contract * * - Rust: [Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and) */ -infix fun Result.and(result: Result): Result { +public infix fun Result.and(result: Result): Result { return when (this) { is Ok -> result is Err -> this @@ -16,7 +16,7 @@ infix fun Result.and(result: Result): Result { } @Deprecated("Use andThen instead", ReplaceWith("andThen { result() }")) -inline infix fun Result.and(result: () -> Result): Result { +public inline infix fun Result.and(result: () -> Result): Result { contract { callsInPlace(result, InvocationKind.AT_MOST_ONCE) } @@ -31,7 +31,7 @@ inline infix fun Result.and(result: () -> Result): Result Result.andThen(transform: (V) -> Result): Result { +public inline infix fun Result.andThen(transform: (V) -> Result): Result { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt index b586634..36bdbff 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt @@ -24,7 +24,7 @@ import kotlin.contracts.contract * * @sample com.github.michaelbull.result.BindingTest */ -inline fun binding(crossinline block: ResultBinding.() -> V): Result { +public inline fun binding(crossinline block: ResultBinding.() -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } @@ -40,8 +40,8 @@ inline fun binding(crossinline block: ResultBinding.() -> V): Result { - fun Result.bind(): V +public interface ResultBinding { + public fun Result.bind(): V } @PublishedApi diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt index d2e0b72..53a5ed8 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt @@ -8,7 +8,7 @@ import kotlin.contracts.contract * invocation was successful, catching and encapsulating any thrown exception * as a failure. */ -inline fun runCatching(block: () -> V): Result { +public inline fun runCatching(block: () -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } @@ -25,7 +25,7 @@ inline fun runCatching(block: () -> V): Result { * returns its encapsulated result if invocation was successful, catching and * encapsulating any thrown exception as a failure. */ -inline infix fun T.runCatching(block: T.() -> V): Result { +public inline infix fun T.runCatching(block: T.() -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } @@ -41,7 +41,7 @@ inline infix fun T.runCatching(block: T.() -> V): Result { * Converts a nullable of type [V] to a [Result]. Returns [Ok] if the value is * non-null, otherwise the supplied [error]. */ -inline infix fun V?.toResultOr(error: () -> E): Result { +public inline infix fun V?.toResultOr(error: () -> E): Result { contract { callsInPlace(error, InvocationKind.AT_MOST_ONCE) } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Get.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Get.kt index 32035b0..af46c06 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Get.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Get.kt @@ -9,7 +9,7 @@ import kotlin.contracts.contract * - Elm: [Result.toMaybe](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#toMaybe) * - Rust: [Result.ok](https://doc.rust-lang.org/std/result/enum.Result.html#method.ok) */ -fun Result.get(): V? { +public fun Result.get(): V? { contract { returnsNotNull() implies (this@get is Ok) returns(null) implies (this@get is Err) @@ -26,7 +26,7 @@ fun Result.get(): V? { * * - Rust: [Result.err](https://doc.rust-lang.org/std/result/enum.Result.html#method.err) */ -fun Result.getError(): E? { +public fun Result.getError(): E? { contract { returns(null) implies (this@getError is Ok) returnsNotNull() implies (this@getError is Err) @@ -48,7 +48,7 @@ fun Result.getError(): E? { * @param default The value to return if [Err]. * @return The [value][Ok.value] if [Ok], otherwise [default]. */ -infix fun Result.getOr(default: V): V { +public infix fun Result.getOr(default: V): V { return when (this) { is Ok -> value is Err -> default @@ -56,7 +56,7 @@ infix fun Result.getOr(default: V): V { } @Deprecated("Use getOrElse instead", ReplaceWith("getOrElse { default() }")) -inline infix fun Result.getOr(default: () -> V): V { +public inline infix fun Result.getOr(default: () -> V): V { contract { callsInPlace(default, InvocationKind.AT_MOST_ONCE) } @@ -72,7 +72,7 @@ inline infix fun Result.getOr(default: () -> V): V { * @param default The error to return if [Ok]. * @return The [error][Err.error] if [Err], otherwise [default]. */ -infix fun Result.getErrorOr(default: E): E { +public infix fun Result.getErrorOr(default: E): E { return when (this) { is Ok -> default is Err -> error @@ -80,7 +80,7 @@ infix fun Result.getErrorOr(default: E): E { } @Deprecated("Use getOrElse instead", ReplaceWith("getErrorOrElse { default() }")) -inline infix fun Result.getErrorOr(default: () -> E): E { +public inline infix fun Result.getErrorOr(default: () -> E): E { contract { callsInPlace(default, InvocationKind.AT_MOST_ONCE) } @@ -95,7 +95,7 @@ inline infix fun Result.getErrorOr(default: () -> E): E { * - Elm: [Result.extract](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#extract) * - Rust: [Result.unwrap_or_else](https://doc.rust-lang.org/src/core/result.rs.html#735-740) */ -inline infix fun Result.getOrElse(transform: (E) -> V): V { +public inline infix fun Result.getOrElse(transform: (E) -> V): V { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } @@ -110,7 +110,7 @@ inline infix fun Result.getOrElse(transform: (E) -> V): V { * Returns the [error][Err.error] if this [Result] is [Err], otherwise the * [transformation][transform] of the [value][Ok.value]. */ -inline infix fun Result.getErrorOrElse(transform: (V) -> E): E { +public inline infix fun Result.getErrorOrElse(transform: (V) -> E): E { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } @@ -127,7 +127,7 @@ inline infix fun Result.getErrorOrElse(transform: (V) -> E): E { * * - Scala: [MergeableEither.merge](https://www.scala-lang.org/api/2.12.0/scala/util/Either$$MergeableEither.html#merge:A) */ -fun Result.merge(): U { +public fun Result.merge(): U { return when (this) { is Ok -> value is Err -> error diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt index 3cd75b1..32fd4c7 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt @@ -4,7 +4,7 @@ package com.github.michaelbull.result * Accumulates value starting with [initial] value and applying [operation] from left to right to * current accumulator value and each element. */ -inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> Result): Result { +public inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> Result): Result { var accumulator = initial for (element in this) { @@ -21,7 +21,7 @@ inline fun Iterable.fold(initial: R, operation: (acc: R, T) -> Resu * Accumulates value starting with [initial] value and applying [operation] from right to left to * each element and current accumulator value. */ -inline fun List.foldRight(initial: R, operation: (T, acc: R) -> Result): Result { +public inline fun List.foldRight(initial: R, operation: (T, acc: R) -> Result): Result { var accumulator = initial if (!isEmpty()) { @@ -42,7 +42,7 @@ inline fun List.foldRight(initial: R, operation: (T, acc: R) -> Res * * - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#combine) */ -fun combine(vararg results: Result): Result, E> { +public fun combine(vararg results: Result): Result, E> { return results.asIterable().combine() } @@ -51,7 +51,7 @@ fun combine(vararg results: Result): Result, E> { * * - Elm: [Result.Extra.combine](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#combine) */ -fun Iterable>.combine(): Result, E> { +public fun Iterable>.combine(): Result, E> { return Ok(map { when (it) { is Ok -> it.value @@ -66,7 +66,7 @@ fun Iterable>.combine(): Result, E> { * * - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts) */ -fun getAll(vararg results: Result): List { +public fun getAll(vararg results: Result): List { return results.asIterable().getAll() } @@ -76,7 +76,7 @@ fun getAll(vararg results: Result): List { * * - Haskell: [Data.Either.lefts](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:lefts) */ -fun Iterable>.getAll(): List { +public fun Iterable>.getAll(): List { return filterIsInstance>().map { it.value } } @@ -86,7 +86,7 @@ fun Iterable>.getAll(): List { * * - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights) */ -fun getAllErrors(vararg results: Result) = results.asIterable().getAllErrors() +public fun getAllErrors(vararg results: Result): List = results.asIterable().getAllErrors() /** * Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err] elements @@ -94,7 +94,7 @@ fun getAllErrors(vararg results: Result) = results.asIterable().get * * - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights) */ -fun Iterable>.getAllErrors(): List { +public fun Iterable>.getAllErrors(): List { return filterIsInstance>().map { it.error } } @@ -105,7 +105,7 @@ fun Iterable>.getAllErrors(): List { * * - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers) */ -fun partition(vararg results: Result): Pair, List> { +public fun partition(vararg results: Result): Pair, List> { return results.asIterable().partition() } @@ -116,7 +116,7 @@ fun partition(vararg results: Result): Pair, List> { * * - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers) */ -fun Iterable>.partition(): Pair, List> { +public fun Iterable>.partition(): Pair, List> { val values = mutableListOf() val errors = mutableListOf() @@ -135,7 +135,7 @@ fun Iterable>.partition(): Pair, List> { * function to each element in the original collection, returning early with the first [Err] if a * transformation fails. */ -inline fun Iterable.mapResult( +public inline fun Iterable.mapResult( transform: (V) -> Result ): Result, E> { return Ok(map { element -> @@ -151,7 +151,7 @@ inline fun Iterable.mapResult( * the results to the given [destination], returning early with the first [Err] if a * transformation fails. */ -inline fun > Iterable.mapResultTo( +public inline fun > Iterable.mapResultTo( destination: C, transform: (V) -> Result ): Result { @@ -168,7 +168,7 @@ inline fun > Iterable.mapResultTo( * given [transform] function to each element in the original collection, returning early with the * first [Err] if a transformation fails. */ -inline fun Iterable.mapResultNotNull( +public inline fun Iterable.mapResultNotNull( transform: (V) -> Result? ): Result, E> { return Ok(mapNotNull { element -> @@ -185,7 +185,7 @@ inline fun Iterable.mapResultNotNull( * only the non-null results to the given [destination], returning early with the first [Err] if a * transformation fails. */ -inline fun > Iterable.mapResultNotNullTo( +public inline fun > Iterable.mapResultNotNullTo( destination: C, transform: (V) -> Result? ): Result { @@ -203,7 +203,7 @@ inline fun > Iterable.mapResultNot * function to each element and its index in the original collection, returning early with the * first [Err] if a transformation fails. */ -inline fun Iterable.mapResultIndexed( +public inline fun Iterable.mapResultIndexed( transform: (index: Int, V) -> Result ): Result, E> { return Ok(mapIndexed { index, element -> @@ -219,7 +219,7 @@ inline fun Iterable.mapResultIndexed( * and appends the results to the given [destination], returning early with the first [Err] if a * transformation fails. */ -inline fun > Iterable.mapResultIndexedTo( +public inline fun > Iterable.mapResultIndexedTo( destination: C, transform: (index: Int, V) -> Result ): Result { @@ -236,7 +236,7 @@ inline fun > Iterable.mapResultIndexedTo * given [transform] function to each element and its index in the original collection, returning * early with the first [Err] if a transformation fails. */ -inline fun Iterable.mapResultIndexedNotNull( +public inline fun Iterable.mapResultIndexedNotNull( transform: (index: Int, V) -> Result? ): Result, E> { return Ok(mapIndexedNotNull { index, element -> @@ -253,7 +253,7 @@ inline fun Iterable.mapResultIndexedNotNull( * and appends only the non-null results to the given [destination], returning early with the first * [Err] if a transformation fails. */ -inline fun > Iterable.mapResultIndexedNotNullTo( +public inline fun > Iterable.mapResultIndexedNotNullTo( destination: C, transform: (index: Int, V) -> Result? ): Result { diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt index 7efaf2f..74b0ca8 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt @@ -11,7 +11,7 @@ import kotlin.contracts.contract * - Haskell: [Data.Bifunctor.first](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:first) * - Rust: [Result.map](https://doc.rust-lang.org/std/result/enum.Result.html#method.map) */ -inline infix fun Result.map(transform: (V) -> U): Result { +public inline infix fun Result.map(transform: (V) -> U): Result { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } @@ -30,7 +30,7 @@ inline infix fun Result.map(transform: (V) -> U): Result { * - Haskell: [Data.Bifunctor.right](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:second) * - Rust: [Result.map_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err) */ -inline infix fun Result.mapError(transform: (E) -> F): Result { +public inline infix fun Result.mapError(transform: (E) -> F): Result { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } @@ -48,7 +48,7 @@ inline infix fun Result.mapError(transform: (E) -> F): Result Result.mapOr(default: U, transform: (V) -> U): U { +public inline fun Result.mapOr(default: U, transform: (V) -> U): U { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } @@ -66,7 +66,7 @@ inline fun Result.mapOr(default: U, transform: (V) -> U): U { * * - Rust: [Result.map_or_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_or_else) */ -inline fun Result.mapOrElse(default: (E) -> U, transform: (V) -> U): U { +public inline fun Result.mapOrElse(default: (E) -> U, transform: (V) -> U): U { contract { callsInPlace(default, InvocationKind.AT_MOST_ONCE) callsInPlace(transform, InvocationKind.AT_MOST_ONCE) @@ -83,7 +83,7 @@ inline fun Result.mapOrElse(default: (E) -> U, transform: (V) -> * function to each element in the original collection, returning early with the first [Err] if a * transformation fails. */ -inline infix fun Result, E>.mapAll(transform: (V) -> Result): Result, E> { +public inline infix fun Result, E>.mapAll(transform: (V) -> Result): Result, E> { return map { iterable -> iterable.map { element -> when (val transformed = transform(element)) { @@ -102,7 +102,7 @@ inline infix fun Result, E>.mapAll(transform: (V) -> Resul * - Elm: [Result.Extra.mapBoth](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#mapBoth) * - Haskell: [Data.Either.either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:either) */ -inline fun Result.mapBoth(success: (V) -> U, failure: (E) -> U): U { +public inline fun Result.mapBoth(success: (V) -> U, failure: (E) -> U): U { contract { callsInPlace(success, InvocationKind.AT_MOST_ONCE) callsInPlace(failure, InvocationKind.AT_MOST_ONCE) @@ -124,7 +124,7 @@ inline fun Result.mapBoth(success: (V) -> U, failure: (E) -> U): * - Elm: [Result.Extra.mapBoth](http://package.elm-lang.org/packages/elm-community/result-extra/2.2.0/Result-Extra#mapBoth) * - Haskell: [Data.Either.either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:either) */ -inline fun Result.fold(success: (V) -> U, failure: (E) -> U): U { +public inline fun Result.fold(success: (V) -> U, failure: (E) -> U): U { contract { callsInPlace(success, InvocationKind.AT_MOST_ONCE) callsInPlace(failure, InvocationKind.AT_MOST_ONCE) @@ -139,7 +139,7 @@ inline fun Result.fold(success: (V) -> U, failure: (E) -> U): U * * - Haskell: [Data.Bifunctor.Bimap](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:bimap) */ -inline fun Result.mapEither(success: (V) -> U, failure: (E) -> F): Result { +public inline fun Result.mapEither(success: (V) -> U, failure: (E) -> F): Result { contract { callsInPlace(success, InvocationKind.AT_MOST_ONCE) callsInPlace(failure, InvocationKind.AT_MOST_ONCE) @@ -159,7 +159,7 @@ inline fun Result.mapEither(success: (V) -> U, failure: (E) - * * - Scala: [Either.flatMap](http://www.scala-lang.org/api/2.12.0/scala/util/Either.html#flatMap[AA>:A,Y](f:B=>scala.util.Either[AA,Y]):scala.util.Either[AA,Y]) */ -inline infix fun Result.flatMap(transform: (V) -> Result): Result { +public inline infix fun Result.flatMap(transform: (V) -> Result): Result { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } @@ -173,7 +173,7 @@ inline infix fun Result.flatMap(transform: (V) -> Result): * * @see [takeIf] */ -inline fun Result.toErrorIf(predicate: (V) -> Boolean, transform: (V) -> E): Result { +public inline fun Result.toErrorIf(predicate: (V) -> Boolean, transform: (V) -> E): Result { contract { callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) callsInPlace(transform, InvocationKind.AT_MOST_ONCE) @@ -195,7 +195,7 @@ inline fun Result.toErrorIf(predicate: (V) -> Boolean, transform: ( * * @see [takeUnless] */ -inline fun Result.toErrorUnless(predicate: (V) -> Boolean, transform: (V) -> E): Result { +public inline fun Result.toErrorUnless(predicate: (V) -> Boolean, transform: (V) -> E): Result { contract { callsInPlace(predicate, InvocationKind.AT_MOST_ONCE) callsInPlace(transform, InvocationKind.AT_MOST_ONCE) diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/On.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/On.kt index d65a0a2..34cf671 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/On.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/On.kt @@ -6,7 +6,7 @@ import kotlin.contracts.contract /** * Invokes an [action] if this [Result] is [Ok]. */ -inline infix fun Result.onSuccess(action: (V) -> Unit): Result { +public inline infix fun Result.onSuccess(action: (V) -> Unit): Result { contract { callsInPlace(action, InvocationKind.AT_MOST_ONCE) } @@ -21,7 +21,7 @@ inline infix fun Result.onSuccess(action: (V) -> Unit): Result Result.onFailure(action: (E) -> Unit): Result { +public inline infix fun Result.onFailure(action: (E) -> Unit): Result { contract { callsInPlace(action, InvocationKind.AT_MOST_ONCE) } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt index b74d540..74a4a27 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt @@ -8,7 +8,7 @@ import kotlin.contracts.contract * * - Rust: [Result.or](https://doc.rust-lang.org/std/result/enum.Result.html#method.or) */ -infix fun Result.or(result: Result): Result { +public infix fun Result.or(result: Result): Result { return when (this) { is Ok -> this is Err -> result @@ -16,7 +16,7 @@ infix fun Result.or(result: Result): Result { } @Deprecated("Use orElse instead", ReplaceWith("orElse { result() }")) -inline infix fun Result.or(result: () -> Result): Result { +public inline infix fun Result.or(result: () -> Result): Result { contract { callsInPlace(result, InvocationKind.AT_MOST_ONCE) } @@ -30,7 +30,7 @@ inline infix fun Result.or(result: () -> Result): Result Result.orElse(transform: (E) -> Result): Result { +public inline infix fun Result.orElse(transform: (E) -> Result): Result { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } @@ -45,7 +45,7 @@ inline infix fun Result.orElse(transform: (E) -> Result): Res * Returns the [transformation][transform] of the [error][Err.error] if this [Result] is [Err], * otherwise this [Ok]. */ -inline infix fun Result.recover(transform: (E) -> V): Ok { +public inline infix fun Result.recover(transform: (E) -> V): Ok { contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt index 054721c..3541325 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt @@ -7,19 +7,19 @@ package com.github.michaelbull.result * - Haskell: [Data.Either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html) * - Rust: [Result](https://doc.rust-lang.org/std/result/enum.Result.html) */ -sealed class Result { +public sealed class Result { - abstract operator fun component1(): V? - abstract operator fun component2(): E? + public abstract operator fun component1(): V? + public abstract operator fun component2(): E? - companion object { + public companion object { /** * Invokes a [function] and wraps it in a [Result], returning an [Err] * if an [Exception] was thrown, otherwise [Ok]. */ @Deprecated("Use top-level runCatching instead", ReplaceWith("runCatching(function)")) - inline fun of(function: () -> V): Result { + public inline fun of(function: () -> V): Result { return try { Ok(function.invoke()) } catch (ex: Exception) { @@ -32,10 +32,10 @@ sealed class Result { /** * Represents a successful [Result], containing a [value]. */ -class Ok(val value: V) : Result() { +public class Ok(public val value: V) : Result() { - override fun component1() = value - override fun component2() = null + override fun component1(): V = value + override fun component2(): Nothing? = null override fun equals(other: Any?): Boolean { if (this === other) return true @@ -48,17 +48,17 @@ class Ok(val value: V) : Result() { return true } - override fun hashCode() = value.hashCode() - override fun toString() = "Ok($value)" + override fun hashCode(): Int = value.hashCode() + override fun toString(): String = "Ok($value)" } /** * Represents a failed [Result], containing an [error]. */ -class Err(val error: E) : Result() { +public class Err(public val error: E) : Result() { - override fun component1() = null - override fun component2() = error + override fun component1(): Nothing? = null + override fun component2(): E = error override fun equals(other: Any?): Boolean { if (this === other) return true @@ -71,6 +71,6 @@ class Err(val error: E) : Result() { return true } - override fun hashCode() = error.hashCode() - override fun toString() = "Err($error)" + override fun hashCode(): Int = error.hashCode() + override fun toString(): String = "Err($error)" } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/ResultIterator.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/ResultIterator.kt index aecb10a..8c7e0c3 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/ResultIterator.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/ResultIterator.kt @@ -6,7 +6,7 @@ package com.github.michaelbull.result * * - Rust: [Result.iter](https://doc.rust-lang.org/std/result/enum.Result.html#method.iter) */ -fun Result.iterator(): Iterator { +public fun Result.iterator(): Iterator { return ResultIterator(this) } @@ -16,7 +16,7 @@ fun Result.iterator(): Iterator { * * - Rust: [Result.iter_mut](https://doc.rust-lang.org/std/result/enum.Result.html#method.iter_mut) */ -fun Result.mutableIterator(): MutableIterator { +public fun Result.mutableIterator(): MutableIterator { return ResultIterator(this) } diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt index caa3582..283fbbb 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt @@ -3,7 +3,7 @@ package com.github.michaelbull.result import kotlin.contracts.InvocationKind import kotlin.contracts.contract -class UnwrapException(message: String) : Exception(message) +public class UnwrapException(message: String) : Exception(message) /** * Unwraps a [Result], yielding the [value][Ok.value]. @@ -12,7 +12,7 @@ class UnwrapException(message: String) : Exception(message) * * @throws UnwrapException if the [Result] is an [Err], with a message containing the [error][Err.error]. */ -fun Result.unwrap(): V { +public fun Result.unwrap(): V { contract { returns() implies (this@unwrap is Ok) } @@ -24,7 +24,7 @@ fun Result.unwrap(): V { } @Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expect { message }")) -infix fun Result.expect(message: String): V { +public infix fun Result.expect(message: String): V { contract { returns() implies (this@expect is Ok) } @@ -40,7 +40,7 @@ infix fun Result.expect(message: String): V { * @param message The message to include in the [UnwrapException] if the [Result] is an [Err]. * @throws UnwrapException if the [Result] is an [Err], with the specified [message]. */ -inline infix fun Result.expect(message: () -> Any): V { +public inline infix fun Result.expect(message: () -> Any): V { contract { callsInPlace(message, InvocationKind.AT_MOST_ONCE) returns() implies (this@expect is Ok) @@ -59,7 +59,7 @@ inline infix fun Result.expect(message: () -> Any): V { * * @throws UnwrapException if the [Result] is [Ok], with a message containing the [value][Ok.value]. */ -fun Result.unwrapError(): E { +public fun Result.unwrapError(): E { contract { returns() implies (this@unwrapError is Err) } @@ -71,7 +71,7 @@ fun Result.unwrapError(): E { } @Deprecated("Use lazy-evaluating variant instead", ReplaceWith("expectError { message }")) -infix fun Result.expectError(message: String): E { +public infix fun Result.expectError(message: String): E { contract { returns() implies (this@expectError is Err) } @@ -87,7 +87,7 @@ infix fun Result.expectError(message: String): E { * @param message The message to include in the [UnwrapException] if the [Result] is [Ok]. * @throws UnwrapException if the [Result] is [Ok], with the specified [message]. */ -inline infix fun Result.expectError(message: () -> Any): E { +public inline infix fun Result.expectError(message: () -> Any): E { contract { callsInPlace(message, InvocationKind.AT_MOST_ONCE) returns() implies (this@expectError is Err) diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt index 67281bf..567cd45 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt @@ -11,7 +11,7 @@ private typealias Producer = () -> Result * * - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map2 */ -inline fun zip( +public inline fun zip( result1: Producer, result2: Producer, transform: (T1, T2) -> V @@ -35,7 +35,7 @@ inline fun zip( * * - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map3 */ -inline fun zip( +public inline fun zip( result1: Producer, result2: Producer, result3: Producer, @@ -63,7 +63,7 @@ inline fun zip( * * - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map4 */ -inline fun zip( +public inline fun zip( result1: Producer, result2: Producer, result3: Producer, @@ -95,7 +95,7 @@ inline fun zip( * * - Elm: http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map5 */ -inline fun zip( +public inline fun zip( result1: Producer, result2: Producer, result3: Producer, diff --git a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/SuspendableBinding.kt b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/SuspendableBinding.kt index 82e0d53..2784ec2 100644 --- a/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/SuspendableBinding.kt +++ b/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/SuspendableBinding.kt @@ -17,7 +17,7 @@ import kotlin.contracts.contract "Please import the kotlin-result-coroutines library to continue using this feature.", level = DeprecationLevel.WARNING ) -suspend inline fun binding(crossinline block: suspend ResultBinding.() -> V): Result { +public suspend inline fun binding(crossinline block: suspend ResultBinding.() -> V): Result { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }