mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-17 05:43:50 +00:00
optional workaround
This commit is contained in:
parent
bcbab3b380
commit
e337cd98c8
@ -4,6 +4,7 @@
|
||||
|
||||
* `Common`:
|
||||
* Ranges intersection functionality
|
||||
* New type `Optional`
|
||||
* `Pagination`:
|
||||
* `Pagination` now extends `ClosedRange<Int>`
|
||||
* `Pagination` intersection functionality
|
||||
|
@ -50,3 +50,24 @@ fun <T> Optional<T>.onPresented(block: (T) -> Unit): Optional<T> = apply {
|
||||
fun <T> Optional<T>.onAbsent(block: () -> Unit): Optional<T> = apply {
|
||||
if (!dataPresented) { block() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or null otherwise
|
||||
*/
|
||||
fun <T> Optional<T>.dataOrNull() = if (dataPresented) data as T else null
|
||||
|
||||
/**
|
||||
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or throw [throwable] otherwise
|
||||
*/
|
||||
fun <T> Optional<T>.dataOrThrow(throwable: Throwable) = if (dataPresented) data as T else throw throwable
|
||||
|
||||
|
||||
/**
|
||||
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or call [block] and returns the result of it
|
||||
*/
|
||||
fun <T> Optional<T>.dataOrElse(block: () -> T) = if (dataPresented) data as T else block()
|
||||
|
||||
/**
|
||||
* Returns [Optional.data] if [Optional.dataPresented] of [this] is true, or call [block] and returns the result of it
|
||||
*/
|
||||
suspend fun <T> Optional<T>.dataOrElseSuspendable(block: suspend () -> T) = if (dataPresented) data as T else block()
|
||||
|
Loading…
Reference in New Issue
Block a user