From 7948aa3ecf97935075b3186608cb6a6bb6016a2c Mon Sep 17 00:00:00 2001 From: Simon Vergauwen Date: Fri, 24 Feb 2023 19:40:31 +0100 Subject: [PATCH] Deprecate or, orElse and unzip --- .../commonMain/kotlin/arrow/core/Option.kt | 53 +++++++++++++------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt index cab16616cf7..df14822bbc0 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt @@ -390,10 +390,21 @@ public sealed class Option { } @JvmStatic + @Deprecated( + RedundantAPI + "Prefer explicitly creating lambdas", + ReplaceWith("{ it.map(f) }") + ) public fun lift(f: (A) -> B): (Option) -> Option = { it.map(f) } } + @Deprecated( + "Prefer using the inline option DSL", + ReplaceWith( + "option { Pair(bind(), b.bind()) }", + "arrow.core.raise.option" + ) + ) public fun zip(other: Option): Option> = zip(other, ::Pair) @@ -1254,20 +1265,19 @@ public inline fun Option.getOrElse(default: () -> T): T { * * @param alternative the default option if this is empty. */ -public inline fun Option.orElse(alternative: () -> Option): Option { - contract { callsInPlace(alternative, InvocationKind.AT_MOST_ONCE) } - return if (isEmpty()) alternative() else this -} +@Deprecated( + NicheAPI + "Prefer using the recover method", + ReplaceWith("recover { alternative().bind() }", "arrow.core.recover") +) +public inline fun Option.orElse(alternative: () -> Option): Option = + recover { alternative().bind() } @Deprecated( - NicheAPI + "Prefer using the orElse method", - ReplaceWith("orElse(value)") + NicheAPI + "Prefer using the recover method", + ReplaceWith("recover { value.bind() }", "arrow.core.recover") ) -public infix fun Option.or(value: Option): Option = if (isEmpty()) { - value -} else { - this -} +public infix fun Option.or(value: Option): Option = + recover { value.bind() } public fun T?.toOption(): Option = this?.let { Some(it) } ?: None @@ -1561,14 +1571,25 @@ public fun Option>.uniteValidated(): Option = validated.fold({ None }, { b -> Some(b) }) } +@Deprecated( + NicheAPI + "Prefer using fold, when or Option DSL", + ReplaceWith( + "fold({ None to None }, { (a, b) -> Some(a) to Some(b) })", + "arrow.core.Option", "arrow.core.Some", "arrow.core.None" + ) +) public fun Option>.unzip(): Pair, Option> = - unzip(::identity) + fold({ None to None }, { (a, b) -> Some(a) to Some(b) }) -public inline fun Option.unzip(f: (C) -> Pair): Pair, Option> = - fold( - { None to None }, - { f(it).let { pair -> Some(pair.first) to Some(pair.second) } } +@Deprecated( + NicheAPI + "Prefer using fold, when or Option DSL", + ReplaceWith( + "fold({ None to None }, { f(it).let { (a, b) -> Some(a) to Some(b) } })", + "arrow.core.Option", "arrow.core.Some", "arrow.core.None" ) +) +public inline fun Option.unzip(f: (C) -> Pair): Pair, Option> = + fold({ None to None }, { f(it).let { (a, b) -> Some(a) to Some(b) } }) /** * Given [A] is a sub type of [B], re-type this value from Option to Option