Rename SuccessOrFailure to Result and hide Failure from ABI

* 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
This commit is contained in:
Roman Elizarov
2018-09-09 11:34:31 +03:00
parent 69ee88871f
commit e2713501ce
81 changed files with 968 additions and 612 deletions

View File

@@ -4,7 +4,7 @@
import kotlin.coroutines.*
import kotlin.coroutines.intrinsics.*
suspend fun suspendHere(): String = suspendCoroutineUninterceptedOrReturn { x ->
x.resumeWith(SuccessOrFailure.success("OK"))
x.resumeWith(Result.success("OK"))
}
suspend fun suspendThere(param: Int, param2: String, param3: Long): String {

View File

@@ -0,0 +1,37 @@
// 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

View File

@@ -1,36 +0,0 @@
// WITH_COROUTINES
// FILE: test.kt
fun testSoF() {
val sof = SuccessOrFailure.success("yes!")
val failure = SuccessOrFailure.failure<String>(Exception())
if (sof.isSuccess) println("success")
if (sof.isFailure) println("failure")
println(sof.getOrThrow())
println(failure.getOrNull())
println(failure.exceptionOrNull())
val other = SuccessOrFailure.success("nope")
if (sof == other) println("==")
if (sof != other) println("!=")
if (sof.equals(other)) println("equals")
if (!sof.equals(other)) println("!equals")
println(sof.hashCode())
println(sof.toString())
println("$sof")
val ans1 = runCatching { 42 }
println(ans1)
val ans2 = 42.runCatching { this }
println(ans2)
println(sof.getOrElse { "oops" })
println(sof.getOrDefault("oops"))
}
// @TestKt.class:
// 0 SuccessOrFailure\$Erased
// 0 SuccessOrFailure\-Erased
// 58 SuccessOrFailure