mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-09 00:21:47 +00:00
* The members of Result are isSuccess, isFailure, exceptionOrNull, getOrNull * The rest of API is implemented via inline-only extensions * There are two internal functions to hide detailed mechanics of an internal Result.Failure class: createFailure and throwOnFailure * Result.toString is explicit: either Success(v) or Failure(x) See KT-26538
35 lines
739 B
Kotlin
Vendored
35 lines
739 B
Kotlin
Vendored
// !LANGUAGE: +InlineClasses
|
|
// IGNORE_BACKEND: JVM_IR
|
|
|
|
inline class Result<T>(val a: Any?) {
|
|
fun getOrThrow(): T = a as T
|
|
}
|
|
|
|
abstract class ResultReceiver<T> {
|
|
abstract fun receive(result: Result<T>)
|
|
}
|
|
|
|
inline fun <T> ResultReceiver(crossinline f: (Result<T>) -> Unit): ResultReceiver<T> =
|
|
object : ResultReceiver<T>() {
|
|
override fun receive(result: Result<T>) {
|
|
f(result)
|
|
}
|
|
}
|
|
|
|
fun test() {
|
|
var invoked = false
|
|
val receiver = ResultReceiver<String> { result ->
|
|
val intResult = result.getOrThrow()
|
|
invoked = true
|
|
}
|
|
|
|
receiver.receive(Result("42"))
|
|
if (!invoked) {
|
|
throw RuntimeException("Fail")
|
|
}
|
|
}
|
|
|
|
fun box(): String {
|
|
test()
|
|
return "OK"
|
|
} |