Created
March 16, 2021 04:54
-
-
Save bijukunjummen/7b5806e359819ebf8008f866f68e2211 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Try<out T> { | |
... | |
abstract fun <R> flatMap(tryBlock: (T) -> Try<R>): Try<R> | |
data class Success<T>(private val result: T) : Try<T>() { | |
... | |
override fun <R> flatMap(tryBlock: (T) -> Try<R>): Try<R> { | |
return try { | |
tryBlock(result) | |
} catch (e: Throwable) { | |
failure(e) | |
} | |
} | |
} | |
data class Failure<T>(private val throwable: Throwable) : Try<T>() { | |
... | |
override fun <R> flatMap(tryBlock: (T) -> Try<R>): Try<R> { | |
return this as Failure<R> | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment