Skip to content

Commit

Permalink
chore: add or and orElse to Result class
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed May 5, 2023
1 parent 6f072fb commit 1819fcf
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,18 @@ fun <V, E> Result<V, E>.getOrElse(function: (E) -> V): V {
}
}

fun <V, E> Result<V, E>.orElse(function: (E) -> Result<V, E>): Result<V, E> {
return when (this) {
is Result.Err -> function(error)
is Result.Ok -> this
}
}

fun <V, E> Result<V, E>.or(default: Result<V, E>): Result<V, E> {
return when (this) {
is Result.Err -> default
is Result.Ok -> this
}
}

class UnwrapException(message: String): RuntimeException(message)

0 comments on commit 1819fcf

Please sign in to comment.