mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-08 15:53:19 +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
37 lines
959 B
Kotlin
Vendored
37 lines
959 B
Kotlin
Vendored
// WITH_COROUTINES
|
|
// FILE: test.kt
|
|
fun test() {
|
|
val result = Result.success("yes!")
|
|
val failure = Result.failure<String>(Exception())
|
|
|
|
if (result.isSuccess) println("success")
|
|
if (result.isFailure) println("failure")
|
|
println(result.getOrThrow())
|
|
println(failure.getOrNull())
|
|
println(failure.exceptionOrNull())
|
|
|
|
val other = Result.success("nope")
|
|
if (result == other) println("==")
|
|
if (result != other) println("!=")
|
|
if (result.equals(other)) println("equals")
|
|
if (!result.equals(other)) println("!equals")
|
|
|
|
println(result.hashCode())
|
|
println(result.toString())
|
|
println("$result")
|
|
|
|
val ans1 = runCatching { 42 }
|
|
println(ans1)
|
|
|
|
val ans2 = 42.runCatching { this }
|
|
println(ans2)
|
|
|
|
println(result.getOrElse { "oops" })
|
|
println(result.getOrDefault("oops"))
|
|
}
|
|
|
|
// @TestKt.class:
|
|
// 0 INVOKESTATIC Result.box-impl
|
|
// 0 INVOKESTATIC Result.unbox-impl
|
|
// 0 Result\$Failure
|
|
// 53 Result |