SHARE
TWEET

Untitled

a guest May 27th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS")
  2. inline class Result<out T : Any, out E : Any> @PublishedApi internal constructor(
  3.     @PublishedApi
  4.     @JvmField
  5.     internal val value: Any
  6. ) {
  7.    
  8.     companion object {
  9.         @Suppress("NOTHING_TO_INLINE")
  10.         inline fun <T : Any, E : Any> Ok(value: T): Result<T, E> = Result(value)
  11.  
  12.         @Suppress("NOTHING_TO_INLINE")
  13.         inline fun <T : Any, E : Any> Err(err: E): Result<T, E> = Result(Result.Error(err))
  14.  
  15.         @Suppress("UNCHECKED_CAST")
  16.         inline fun <T : Any> of(body: () -> T): Result<T, Throwable> =
  17.             try {
  18.                 Ok(body())
  19.             } catch (tr: Throwable) {
  20.                 Err(tr)
  21.             }
  22.     }
  23.  
  24.     @Suppress("UNCHECKED_CAST")
  25.     inline fun <R> reduce(okBody: (T) -> R, errBody: (E) -> R): R =
  26.         if (value !is Error<*>) okBody(value as T) else errBody(value.err as E)
  27.  
  28.     inline val isOk: Boolean
  29.         get() = reduce(
  30.             okBody = { true },
  31.             errBody = { false }
  32.         )
  33.  
  34.     inline val isErr: Boolean
  35.         get() = reduce(
  36.             okBody = { false },
  37.             errBody = { true }
  38.         )
  39.  
  40.     inline val okValue: T?
  41.         get() = reduce(
  42.             okBody = { it },
  43.             errBody = { null }
  44.         )
  45.  
  46.     inline val errValue: E?
  47.         get() = reduce(
  48.             okBody = { null },
  49.             errBody = { it }
  50.         )
  51.  
  52.     @Suppress("UNCHECKED_CAST")
  53.     inline fun <R : Any> map(body: (T) -> R): Result<R, E> = reduce(
  54.         okBody = { Ok(body(it)) },
  55.         errBody = { this as Result<R, E> }
  56.     )
  57.  
  58.     @Suppress("UNCHECKED_CAST")
  59.     inline fun <R : Any> mapErr(body: (E) -> R): Result<T, R> = reduce(
  60.         okBody = { this as Result<T, R> },
  61.         errBody = { Err(body(it)) }
  62.     )
  63.  
  64.     @Suppress("NOTHING_TO_INLINE")
  65.     inline fun asSequence(): Sequence<T> = reduce(
  66.         okBody = { sequenceOf(it) },
  67.         errBody = { emptySequence() }
  68.     )
  69.  
  70.     @PublishedApi
  71.     internal class Error<E : Any>(
  72.         @JvmField
  73.         val err: E
  74.     )
  75. }
  76.  
  77. @Suppress("NOTHING_TO_INLINE")
  78. inline fun <T: Any, E: Any> Result<T, E>.orDefault(defaultValue: T): T = reduce(
  79.     okBody = { it },
  80.     errBody = { defaultValue }
  81. )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top