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

[PROPOSAL] Option API deprecation, and preparation for 2.x.x #2913

Merged
merged 33 commits into from
Feb 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c675811
Deprecate foldable methods
franciscodr Feb 2, 2023
268c995
Deprecate functor/applicative/monad methods
franciscodr Feb 4, 2023
8858cec
Update API files
franciscodr Feb 4, 2023
ea87f40
Apply suggestions from code review
franciscodr Feb 4, 2023
fb8a3aa
Add getOrNull method
franciscodr Feb 4, 2023
439773d
Update API files
franciscodr Feb 4, 2023
2d4cb40
Restore isEmpty implementation
franciscodr Feb 4, 2023
4165ee9
Update API files
franciscodr Feb 4, 2023
8e8c092
Deprecate applicative/monad error methods
franciscodr Feb 4, 2023
78b4384
Deprecate utility methods
franciscodr Feb 5, 2023
e9ce01b
Merge remote-tracking branch 'origin/main' into fd-option-API
franciscodr Feb 6, 2023
37bbc40
Merge remote-tracking branch 'origin/main' into fd-option-API
franciscodr Feb 6, 2023
356a273
Deprecate assign methods
franciscodr Feb 6, 2023
7fc257e
Fix outdated knit files
franciscodr Feb 6, 2023
3288486
Merge branch 'main' into fd-option-API
franciscodr Feb 6, 2023
1d1ae2c
Merge branch 'main' into fd-option-API
nomisRev Feb 6, 2023
8198fce
Using the inline option DSL
franciscodr Feb 6, 2023
b0f9ff1
Change redeem implementation for Either and Option
franciscodr Feb 6, 2023
b05b8ea
Merge branch 'main' into fd-option-API
franciscodr Feb 6, 2023
49a018b
Fix wrong deprecation replacement
franciscodr Feb 7, 2023
58b51a4
Revert Nullable.toOption deprecation
franciscodr Feb 7, 2023
3936647
Add missing contracts
franciscodr Feb 7, 2023
241d601
Update API files
franciscodr Feb 7, 2023
b4fad18
Merge branch 'main' into fd-option-API
nomisRev Feb 8, 2023
d4f5bc4
Revert filterIsInstance deprecation
franciscodr Feb 8, 2023
df95be0
Add isNone and isSome methods for Option
franciscodr Feb 8, 2023
6599bf3
Update API files
franciscodr Feb 8, 2023
6408a19
Trigger build
franciscodr Feb 9, 2023
4a9c115
Update API files
franciscodr Feb 9, 2023
965395b
Convert isSome into an instance method
franciscodr Feb 9, 2023
3abcb1e
Restore abstract isEmpty function for Option
franciscodr Feb 9, 2023
2911df7
Merge branch 'main' into fd-option-API
franciscodr Feb 9, 2023
cf57557
Merge branch 'main' into fd-option-API
nomisRev Feb 9, 2023
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
158 changes: 84 additions & 74 deletions arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt
Original file line number Diff line number Diff line change
Expand Up @@ -397,41 +397,35 @@ public sealed class Option<out A> {
public fun <B> zip(other: Option<B>): Option<Pair<A, B>> =
zip(other, ::Pair)

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C> zip(
b: Option<B>,
map: (A, B) -> C
): Option<C> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return zip(
b,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit
) { b, c, _, _, _, _, _, _, _, _ -> map(b, c) }
return option { map(bind(), b.bind()) }
}

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind(), c.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C, D> zip(
b: Option<B>,
c: Option<C>,
map: (A, B, C) -> D
): Option<D> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return zip(
b,
c,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit
) { b, c, d, _, _, _, _, _, _, _ -> map(b, c, d) }
return option { map(bind(), b.bind(), c.bind()) }
}

/**
Expand Down Expand Up @@ -540,26 +534,30 @@ public sealed class Option<out A> {
return onSome(f)
}

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind(), c.bind(), d.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C, D, E> zip(
b: Option<B>,
c: Option<C>,
d: Option<D>,
map: (A, B, C, D) -> E
): Option<E> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return zip(
b,
c,
d,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit,
Some.unit
) { a, b, c, d, _, _, _, _, _, _ -> map(a, b, c, d) }
return option { map(bind(), b.bind(), c.bind(), d.bind()) }
}

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C, D, E, F> zip(
b: Option<B>,
c: Option<C>,
Expand All @@ -568,17 +566,16 @@ public sealed class Option<out A> {
map: (A, B, C, D, E) -> F
): Option<F> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, Some.unit, Some.unit, Some.unit, Some.unit, Some.unit) { a, b, c, d, e, f, _, _, _, _ ->
map(
a,
b,
c,
d,
e
)
}
return option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind()) }
}

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C, D, E, F, G> zip(
b: Option<B>,
c: Option<C>,
Expand All @@ -588,18 +585,16 @@ public sealed class Option<out A> {
map: (A, B, C, D, E, F) -> G
): Option<G> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, Some.unit, Some.unit, Some.unit, Some.unit) { a, b, c, d, e, f, _, _, _, _ ->
map(
a,
b,
c,
d,
e,
f
)
}
return option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind()) }
}

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C, D, E, F, G, H> zip(
b: Option<B>,
c: Option<C>,
Expand All @@ -610,9 +605,16 @@ public sealed class Option<out A> {
map: (A, B, C, D, E, F, G) -> H
): Option<H> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, g, Some.unit, Some.unit, Some.unit) { a, b, c, d, e, f, g, _, _, _ -> map(a, b, c, d, e, f, g) }
return option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind()) }
}

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C, D, E, F, G, H, I> zip(
b: Option<B>,
c: Option<C>,
Expand All @@ -624,9 +626,16 @@ public sealed class Option<out A> {
map: (A, B, C, D, E, F, G, H) -> I
): Option<I> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, g, h, Some.unit, Some.unit) { a, b, c, d, e, f, g, h, _, _ -> map(a, b, c, d, e, f, g, h) }
return option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind()) }
}

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C, D, E, F, G, H, I, J> zip(
b: Option<B>,
c: Option<C>,
Expand All @@ -639,9 +648,16 @@ public sealed class Option<out A> {
map: (A, B, C, D, E, F, G, H, I) -> J
): Option<J> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return zip(b, c, d, e, f, g, h, i, Some.unit) { a, b, c, d, e, f, g, h, i, _ -> map(a, b, c, d, e, f, g, h, i) }
return option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind()) }
}

@Deprecated(
"Prefer using the inline option DSL",
ReplaceWith(
"option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind(), j.bind()) }",
"arrow.core.raise.option"
)
)
public inline fun <B, C, D, E, F, G, H, I, J, K> zip(
b: Option<B>,
c: Option<C>,
Expand All @@ -655,11 +671,7 @@ public sealed class Option<out A> {
map: (A, B, C, D, E, F, G, H, I, J) -> K
): Option<K> {
contract { callsInPlace(map, InvocationKind.AT_MOST_ONCE) }
return if (this is Some && b is Some && c is Some && d is Some && e is Some && f is Some && g is Some && h is Some && i is Some && j is Some) {
Some(map(this.value, b.value, c.value, d.value, e.value, f.value, g.value, h.value, i.value, j.value))
} else {
None
}
return option { map(bind(), b.bind(), c.bind(), d.bind(), e.bind(), f.bind(), g.bind(), h.bind(), i.bind(), j.bind()) }
}

/**
Expand Down Expand Up @@ -1262,26 +1274,25 @@ public inline fun <reified B> Option<*>.filterIsInstance(): Option<B> =
@Deprecated(
NicheAPI + "Prefer using the orElse method",
ReplaceWith(
"orElse { Some(f(Unit)) }",
"arrow.core.Some",
"arrow.core.orElse"
"recover { f(Unit) }",
"arrow.core.recover"
)
)
public inline fun <A> Option<A>.handleError(f: (Unit) -> A): Option<A> {
contract { callsInPlace(f, InvocationKind.AT_MOST_ONCE) }
return handleErrorWith { Some(f(Unit)) }
return recover { f(Unit) }
}

@Deprecated(
NicheAPI + "Prefer using the orElse method",
ReplaceWith(
"orElse { f(Unit) }",
"arrow.core.orElse"
"recover { f(Unit).bind() }",
"arrow.core.recover"
)
)
public inline fun <A> Option<A>.handleErrorWith(f: (Unit) -> Option<A>): Option<A> {
contract { callsInPlace(f, InvocationKind.AT_MOST_ONCE) }
return if (isEmpty()) f(Unit) else this
return recover { f(Unit).bind() }
}

public fun <A> Option<Option<A>>.flatten(): Option<A> =
Expand All @@ -1290,32 +1301,31 @@ public fun <A> Option<Option<A>>.flatten(): Option<A> =
@Deprecated(
NicheAPI + "Prefer using the Option DSL or explicit map with orElse",
ReplaceWith(
"map(fb).orElse { Some(fe(Unit)) }",
"arrow.core.Some",
"arrow.core.orElse"
"map(fb).recover { fe(Unit) }",
franciscodr marked this conversation as resolved.
Show resolved Hide resolved
"arrow.core.recover"
)
)
public inline fun <A, B> Option<A>.redeem(fe: (Unit) -> B, fb: (A) -> B): Option<B> {
contract {
callsInPlace(fe, InvocationKind.AT_MOST_ONCE)
callsInPlace(fb, InvocationKind.AT_MOST_ONCE)
}
return map(fb).handleError(fe)
return map(fb).recover { fe(Unit) }
}

@Deprecated(
NicheAPI + "Prefer using the Option DSL or explicit flatMap with orElse",
ReplaceWith(
"flatMap(fb).orElse(fe)",
"arrow.core.orElse"
"flatMap(fb).recover { fe(Unit).bind() }",
nomisRev marked this conversation as resolved.
Show resolved Hide resolved
"arrow.core.recover"
)
)
public inline fun <A, B> Option<A>.redeemWith(fe: (Unit) -> Option<B>, fb: (A) -> Option<B>): Option<B> {
contract {
callsInPlace(fe, InvocationKind.AT_MOST_ONCE)
callsInPlace(fb, InvocationKind.AT_MOST_ONCE)
}
return flatMap(fb).handleErrorWith(fe)
return flatMap(fb).recover { fe(Unit).bind() }
}

@Deprecated(
Expand Down