Skip to content

Commit

Permalink
Remove crossinline declarations where possible
Browse files Browse the repository at this point in the history
`crossinline` declarations are used when a callback passed to an `inline` method might escape the current execution context, which makes it impossible to use non-local returns. This is not the case for most methods here though, so the declaration can be removed and non-local returns can be used. This can simplify handling some cases:

```
val content = uct.fold(
  onLoading = { return },
  onError = { return },
  onContent = { it }
)
doSomething(content)
```
  • Loading branch information
sheepmaster committed Jun 13, 2024
1 parent 70e8eb4 commit 503ef11
Show file tree
Hide file tree
Showing 23 changed files with 158 additions and 158 deletions.
2 changes: 1 addition & 1 deletion lce/src/main/java/com/laimiux/lce/CE.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface CE<out C, out E> {
*/
inline fun <C : Any, E> fromNullable(
content: C?,
crossinline onNull: () -> CE<C, E>
onNull: () -> CE<C, E>
): CE<C, E> {
return if (content == null) {
onNull()
Expand Down
2 changes: 1 addition & 1 deletion lce/src/main/java/com/laimiux/lce/CT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface CT<out C> {
*/
inline fun <L, C : Any, E> fromNullable(
content: C?,
crossinline onNull: () -> CT<C>
onNull: () -> CT<C>
): CT<C> {
return if (content == null) {
onNull()
Expand Down
14 changes: 7 additions & 7 deletions lce/src/main/java/com/laimiux/lce/ContentOrElse.kt
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
package com.laimiux.lce

inline fun <L, C, E> LCE<L, C, E>.contentOrElse(crossinline onOther: (LE<L, E>) -> C): C {
inline fun <L, C, E> LCE<L, C, E>.contentOrElse(onOther: (LE<L, E>) -> C): C {
return foldContent(
onContent = { it },
onOther = onOther
)
}

inline fun <C, E> UCE<C, E>.contentOrElse(crossinline onOther: (UE<E>) -> C): C {
inline fun <C, E> UCE<C, E>.contentOrElse(onOther: (UE<E>) -> C): C {
return foldContent(
onContent = { it },
onOther = onOther
)
}

inline fun <C> UCT<C>.contentOrElse(crossinline onOther: (UT) -> C): C {
inline fun <C> UCT<C>.contentOrElse(onOther: (UT) -> C): C {
return foldContent(
onContent = { it },
onOther = onOther
)
}

inline fun <C, E> CE<C, E>.contentOrElse(crossinline onOther: () -> C): C {
inline fun <C, E> CE<C, E>.contentOrElse(onOther: () -> C): C {
return fold(
onContent = { it },
onError = { onOther() }
)
}

inline fun <C> CT<C>.contentOrElse(crossinline onOther: () -> C): C {
inline fun <C> CT<C>.contentOrElse(onOther: () -> C): C {
return fold(
onContent = { it },
onError = { onOther() }
)
}

inline fun <L, C> LC<L, C>.contentOrElse(crossinline onOther: () -> C): C {
inline fun <L, C> LC<L, C>.contentOrElse(onOther: () -> C): C {
return fold(
onContent = { it },
onLoading = { onOther() }
)
}

inline fun <C> UC<C>.contentOrElse(crossinline onOther: () -> C): C {
inline fun <C> UC<C>.contentOrElse(onOther: () -> C): C {
return fold(
onContent = { it },
onLoading = { onOther() }
Expand Down
26 changes: 13 additions & 13 deletions lce/src/main/java/com/laimiux/lce/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fun <C, E> LCE<Any?, C, E>.asCE(): CE<C, E>? {
* Converts [LCE] to a [CE] by [mapping][map] the loading state to [CE].
*/
inline fun <L, C, E> LCE<L, C, E>.asCE(
crossinline map: (L) -> CE<C, E>
map: (L) -> CE<C, E>
): CE<C, E> {
return foldTypes(
onLoading = { map(it.value) },
Expand All @@ -60,7 +60,7 @@ fun <C> LCE<Any?, C, Throwable>.asCT(): CT<C>? {
* Converts [LCE] to a [CT] by [mapping][map] the loading state to [CT].
*/
inline fun <L, C> LCE<L, C, Throwable>.asCT(
crossinline map: (L) -> CT<C>
map: (L) -> CT<C>
): CT<C> {
return foldTypes(
onLoading = { map(it.value) },
Expand All @@ -82,7 +82,7 @@ fun <L, C> LCE<L, C, Any?>.asLC(): LC<L, C>? {
* Converts [LCE] to a [LC] by [mapping][map] the error state to [LC].
*/
inline fun <L, C, E> LCE<L, C, E>.asLC(
crossinline fold: (E) -> LC<L, C>
fold: (E) -> LC<L, C>
): LC<L, C> {
return foldError(
onError = fold,
Expand All @@ -104,7 +104,7 @@ fun <C> LCE<Unit, C, Any?>.asUC(): UC<C>? {
* Converts [LCE] to a [UC] by [mapping][map] the error state to [UC].
*/
inline fun <C, E> LCE<Unit, C, E>.asUC(
crossinline fold: (E) -> UC<C>
fold: (E) -> UC<C>
): UC<C> {
return foldError(
onError = fold,
Expand Down Expand Up @@ -146,7 +146,7 @@ fun <C, E> UCE<C, E>.asCE(): CE<C, E>? {
* Converts [UCE] to a [CE] by [mapping][map] the loading state to [CE].
*/
inline fun <C, E> UCE<C, E>.asCE(
crossinline map: () -> CE<C, E>
map: () -> CE<C, E>
): CE<C, E> {
return asLCE().asCE { map() }
}
Expand All @@ -162,7 +162,7 @@ fun <C> UCE<C, Throwable>.asCT(): CT<C>? {
* Converts [UCE] to a [CT] by [mapping][map] the loading state to [CT].
*/
inline fun <C> UCE<C, Throwable>.asCT(
crossinline map: () -> CT<C>
map: () -> CT<C>
): CT<C> {
return asLCE().asCT { map() }
}
Expand All @@ -182,7 +182,7 @@ fun <C, E> UCE<C, E>.asLC(): LC<Unit, C>? {
* Converts [UCE] to a [LC] by [mapping][map] the error state to [LC].
*/
inline fun <C, E> UCE<C, E>.asLC(
crossinline fold: (E) -> LC<Unit, C>
fold: (E) -> LC<Unit, C>
): LC<Unit, C> {
return asLCE().asLC(fold)
}
Expand All @@ -202,7 +202,7 @@ fun <C, E> UCE<C, E>.asUC(): UC<C>? {
* Converts [UCE] to a [UC] by [mapping][map] the error state to [UC].
*/
inline fun <C, E> UCE<C, E>.asUC(
crossinline fold: (E) -> UC<C>
fold: (E) -> UC<C>
): UC<C> {
return asLCE().asUC(fold)
}
Expand Down Expand Up @@ -244,7 +244,7 @@ fun <C> UCT<C>.asCT(): CT<C>? {
* Converts [UCT] to a [CT] by [mapping][map] the loading state to [CT].
*/
inline fun <C> UCT<C>.asCT(
crossinline map: () -> CT<C>
map: () -> CT<C>
): CT<C> {
return asLCE().asCT { map() }
}
Expand Down Expand Up @@ -276,7 +276,7 @@ fun <C, E> UCT<C>.asUCE(map: (throwable: Throwable) -> E): UCE<C, E> {
* Converts [UCT] to a [CE] by [mapping][map] the loading state to [CE].
*/
inline fun <C> UCT<C>.asCE(
crossinline map: () -> CE<C, Throwable>
map: () -> CE<C, Throwable>
): CE<C, Throwable> {
return asLCE().asCE { map() }
}
Expand All @@ -293,7 +293,7 @@ fun <C> UCT<C>.asLC(): LC<Unit, C>? {
* Converts [UCT] to a [LC] by [mapping][map] the error state to [LC].
*/
inline fun <C> UCT<C>.asLC(
crossinline fold: (Throwable) -> LC<Unit, C>
fold: (Throwable) -> LC<Unit, C>
): LC<Unit, C> {
return asLCE().asLC(fold)
}
Expand All @@ -302,7 +302,7 @@ inline fun <C> UCT<C>.asLC(
* Returns null on error, otherwise returns [UC].
*/
inline fun <C> UCT<C>.asUC(
crossinline fold: (Throwable) -> UC<C>
fold: (Throwable) -> UC<C>
): UC<C> {
return asLCE().asUC(fold)
}
Expand All @@ -318,7 +318,7 @@ fun <C> UCT<C>.asUC(): UC<C>? {
* Converts [UCT] to a [UC] by [mapping][map] the error state to [UC].
*/
inline fun <C> UCT<C>.asLC(
crossinline fold: (Throwable) -> UC<C>
fold: (Throwable) -> UC<C>
): UC<C> {
return asLCE().asUC(fold)
}
Expand Down
14 changes: 7 additions & 7 deletions lce/src/main/java/com/laimiux/lce/FlatMapContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package com.laimiux.lce
* ```
*/
inline fun <L, C, E, NewC> LCE<L, C, E>.flatMapContent(
crossinline transform: (C) -> LCE<L, NewC, E>
transform: (C) -> LCE<L, NewC, E>
): LCE<L, NewC, E> {
return foldTypes(
onLoading = { it },
Expand All @@ -24,7 +24,7 @@ inline fun <L, C, E, NewC> LCE<L, C, E>.flatMapContent(
}

inline fun <C, E, NewC> UCE<C, E>.flatMapContent(
crossinline transform: (C) -> UCE<NewC, E>
transform: (C) -> UCE<NewC, E>
): UCE<NewC, E> {
return foldTypes(
onLoading = { it },
Expand All @@ -34,7 +34,7 @@ inline fun <C, E, NewC> UCE<C, E>.flatMapContent(
}

inline fun <C, NewC> UCT<C>.flatMapContent(
crossinline transform: (C) -> UCT<NewC>
transform: (C) -> UCT<NewC>
): UCT<NewC> {
return foldTypes(
onLoading = { it },
Expand All @@ -44,7 +44,7 @@ inline fun <C, NewC> UCT<C>.flatMapContent(
}

inline fun <C, E, NewC> CE<C, E>.flatMapContent(
crossinline transform: (C) -> CE<NewC, E>
transform: (C) -> CE<NewC, E>
): CE<NewC, E> {
return foldTypes(
onContent = { transform(it.value) },
Expand All @@ -53,7 +53,7 @@ inline fun <C, E, NewC> CE<C, E>.flatMapContent(
}

inline fun <C, NewC> CT<C>.flatMapContent(
crossinline transform: (C) -> CT<NewC>
transform: (C) -> CT<NewC>
): CT<NewC> {
return foldTypes(
onContent = { transform(it.value) },
Expand All @@ -62,7 +62,7 @@ inline fun <C, NewC> CT<C>.flatMapContent(
}

inline fun <L, C, NewC> LC<L, C>.flatMapContent(
crossinline transform: (C) -> LC<L, NewC>
transform: (C) -> LC<L, NewC>
): LC<L, NewC> {
return foldTypes(
onLoading = { it },
Expand All @@ -71,7 +71,7 @@ inline fun <L, C, NewC> LC<L, C>.flatMapContent(
}

inline fun <C, NewC> UC<C>.flatMapContent(
crossinline transform: (C) -> UC<NewC>
transform: (C) -> UC<NewC>
): UC<NewC> {
return foldTypes(
onLoading = { it },
Expand Down
10 changes: 5 additions & 5 deletions lce/src/main/java/com/laimiux/lce/FlatMapError.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.laimiux.lce

inline fun <L, C, E, NewE> LCE<L, C, E>.flatMapError(
crossinline map: (E) -> LCE<L, C, NewE>
map: (E) -> LCE<L, C, NewE>
): LCE<L, C, NewE> {
return foldTypes(
onError = { map(it.value) },
Expand All @@ -11,7 +11,7 @@ inline fun <L, C, E, NewE> LCE<L, C, E>.flatMapError(
}

inline fun <C, E, NewE> UCE<C, E>.flatMapError(
crossinline map: (E) -> UCE<C, NewE>
map: (E) -> UCE<C, NewE>
): UCE<C, NewE> {
return foldTypes(
onError = { map(it.value) },
Expand All @@ -21,7 +21,7 @@ inline fun <C, E, NewE> UCE<C, E>.flatMapError(
}

inline fun <C> UCT<C>.flatMapError(
crossinline map: (Throwable) -> UCT<C>
map: (Throwable) -> UCT<C>
): UCT<C> {
return foldTypes(
onError = { map(it.value) },
Expand All @@ -31,7 +31,7 @@ inline fun <C> UCT<C>.flatMapError(
}

inline fun <C, E, NewE> CE<C, E>.flatMapError(
crossinline map: (E) -> CE<C, NewE>
map: (E) -> CE<C, NewE>
): CE<C, NewE> {
return foldTypes(
onError = { map(it.value) },
Expand All @@ -40,7 +40,7 @@ inline fun <C, E, NewE> CE<C, E>.flatMapError(
}

inline fun <C> CT<C>.flatMapError(
crossinline map: (Throwable) -> CT<C>
map: (Throwable) -> CT<C>
): CT<C> {
return foldTypes(
onError = { map(it.value) },
Expand Down
10 changes: 5 additions & 5 deletions lce/src/main/java/com/laimiux/lce/FlatMapLoading.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.laimiux.lce

inline fun <L, C, E, NewL> LCE<L, C, E>.flatMapLoading(
crossinline map: (L) -> LCE<NewL, C, E>
map: (L) -> LCE<NewL, C, E>
): LCE<NewL, C, E> {
return foldTypes(
onLoading = { map(it.value) },
Expand All @@ -11,7 +11,7 @@ inline fun <L, C, E, NewL> LCE<L, C, E>.flatMapLoading(
}

inline fun <C, E> UCE<C, E>.flatMapLoading(
crossinline map: () -> UCE<C, E>
map: () -> UCE<C, E>
): UCE<C, E> {
return foldTypes(
onLoading = { map() },
Expand All @@ -21,7 +21,7 @@ inline fun <C, E> UCE<C, E>.flatMapLoading(
}

inline fun <C> UCT<C>.flatMapLoading(
crossinline map: () -> UCT<C>
map: () -> UCT<C>
): UCT<C> {
return foldTypes(
onLoading = { map() },
Expand All @@ -31,7 +31,7 @@ inline fun <C> UCT<C>.flatMapLoading(
}

inline fun <L, C, NewL> LC<L, C>.flatMapLoading(
crossinline map: () -> LC<NewL, C>
map: () -> LC<NewL, C>
): LC<NewL, C> {
return foldTypes(
onLoading = { map() },
Expand All @@ -40,7 +40,7 @@ inline fun <L, C, NewL> LC<L, C>.flatMapLoading(
}

inline fun <C> UC<C>.flatMapLoading(
crossinline map: () -> UC<C>
map: () -> UC<C>
): UC<C> {
return foldTypes(
onLoading = { map() },
Expand Down
8 changes: 4 additions & 4 deletions lce/src/main/java/com/laimiux/lce/FlatMerge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package com.laimiux.lce
*/
inline fun <C, C2, E, T> UCE<C, E>.flatMerge(
other: UCE<C2, E>,
crossinline merge: (C, C2) -> UCE<T, E>
merge: (C, C2) -> UCE<T, E>
): UCE<T, E> {
return takeFirstError(other) { first, second ->
first.asUCE().flatMapContent { firstContent ->
Expand All @@ -21,7 +21,7 @@ inline fun <C, C2, E, T> UCE<C, E>.flatMerge(
*/
inline fun <C, C2, T> UCT<C>.flatMerge(
other: UCT<C2>,
crossinline merge: (C, C2) -> UCT<T>
merge: (C, C2) -> UCT<T>
): UCT<T> {
return takeFirstError(other) { first, second ->
first.asUCT().flatMapContent { firstContent ->
Expand All @@ -37,7 +37,7 @@ inline fun <C, C2, T> UCT<C>.flatMerge(
*/
inline fun <L, C, C2, T> LC<L, C>.flatMerge(
other: LC<L, C2>,
crossinline merge: (C, C2) -> LC<L, T>
merge: (C, C2) -> LC<L, T>
): LC<L, T> {
return flatMapContent { first ->
other.flatMapContent { second ->
Expand All @@ -51,7 +51,7 @@ inline fun <L, C, C2, T> LC<L, C>.flatMerge(
*/
inline fun <C, C2, T> UC<C>.flatMerge(
other: UC<C2>,
crossinline merge: (C, C2) -> UC<T>,
merge: (C, C2) -> UC<T>,
): UC<T> {
return flatMapContent { first ->
other.flatMapContent { second ->
Expand Down
Loading

0 comments on commit 503ef11

Please sign in to comment.