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
@@ -5,8 +5,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function returning Result with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
@@ -14,8 +14,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function returning Result with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
@@ -23,8 +23,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function 'incorrectCatching' returning 'SuccessOrFailure&lt;Double&gt;' without the corresponding function 'incorrect' returning 'Double'</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function 'incorrectCatching' returning 'Result&lt;Double&gt;' without the corresponding function 'incorrect' returning 'Double'</description>
</problem>
<problem>
<file>test.kt</file>
@@ -32,8 +32,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function 'strangeCatching' returning 'SuccessOrFailure&lt;Boolean&gt;' without the corresponding function 'strange' returning 'Boolean'</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function 'strangeCatching' returning 'Result&lt;Boolean&gt;' without the corresponding function 'strange' returning 'Boolean'</description>
</problem>
<problem>
<file>test.kt</file>
@@ -41,8 +41,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function returning Result with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
@@ -50,8 +50,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function returning Result with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
@@ -59,8 +59,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function 'classIncorrectCatching' returning 'SuccessOrFailure&lt;Double&gt;' without the corresponding function 'classIncorrect' returning 'Double'</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function 'classIncorrectCatching' returning 'Result&lt;Double&gt;' without the corresponding function 'classIncorrect' returning 'Double'</description>
</problem>
<problem>
<file>test.kt</file>
@@ -68,8 +68,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function returning Result with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
@@ -77,8 +77,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function returning Result with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
@@ -86,8 +86,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function returning SuccessOrFailure with a name that does not end with Catching</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function returning Result with a name that does not end with Catching</description>
</problem>
<problem>
<file>test.kt</file>
@@ -95,8 +95,8 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function 'calcComplexCatching' returning 'SuccessOrFailure&lt;Double&gt;' without the corresponding function 'calcComplex' returning 'Double'</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function 'calcComplexCatching' returning 'Result&lt;Double&gt;' without the corresponding function 'calcComplex' returning 'Double'</description>
</problem>
<problem>
<file>test.kt</file>
@@ -104,7 +104,7 @@
<module>light_idea_test_case</module>
<package>&lt;default&gt;</package>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning SuccessOrFailure</problem_class>
<description>Function 'extensionActCatching' returning 'SuccessOrFailure&lt;Int&gt;' without the corresponding function 'extensionAct' returning 'Int'</description>
<problem_class severity="WEAK WARNING" attribute_key="INFO_ATTRIBUTES">Function returning Result</problem_class>
<description>Function 'extensionActCatching' returning 'Result&lt;Int&gt;' without the corresponding function 'extensionAct' returning 'Int'</description>
</problem>
</problems>
@@ -1 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.coroutines.ResultIsSuccessOrFailureInspection
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.coroutines.ResultIsResultInspection
@@ -1,67 +1,67 @@
package kotlin
// NO (constructor)
class SuccessOrFailure<T>(val value: T?) {
class Result<T>(val value: T?) {
fun getOrThrow(): T = value ?: throw AssertionError("")
}
// YES
fun getSuccess() = success()
// YES
fun getSuccessExplicit(): SuccessOrFailure<Int> = SuccessOrFailure(456)
fun getSuccessExplicit(): Result<Int> = Result(456)
// NO (not catching 'correct' available)
fun correctCatching() = SuccessOrFailure(true)
// NO (not SuccessOrFailure)
fun correctCatching() = Result(true)
// NO (not Result)
fun correct() = true
// YES
fun incorrectCatching() = SuccessOrFailure(3.14)
fun incorrectCatching() = Result(3.14)
// YES
fun strangeCatching() = runCatching { false }
// NO (not SuccessOrFailure)
// NO (not Result)
fun strange() = 1
class Container {
// YES
fun classGetSuccess() = SuccessOrFailure("123")
fun classGetSuccess() = Result("123")
// YES
fun classGetSuccessExplicit(): SuccessOrFailure<Int> = SuccessOrFailure(456)
fun classGetSuccessExplicit(): Result<Int> = Result(456)
// NO (not catching 'classCorrect' available)
fun classCorrectCatching() = SuccessOrFailure(true)
// NO (not SuccessOrFailure)
fun classCorrectCatching() = Result(true)
// NO (not Result)
fun classCorrect() = true
// YES
fun classIncorrectCatching() = SuccessOrFailure(3.14)
fun classIncorrectCatching() = Result(3.14)
}
fun test() {
// YES
fun localGetSuccess() = SuccessOrFailure("123")
fun localGetSuccess() = Result("123")
// YES
val anonymous = fun() = SuccessOrFailure(45)
val anonymous = fun() = Result(45)
// YES
val lambda = { SuccessOrFailure(true) }
val lambda = { Result(true) }
// NO yet (we do not report local *catching functions)
fun localCatching() = SuccessOrFailure(2.72)
fun localCatching() = Result(2.72)
}
// NO (stdlib)
fun success() = SuccessOrFailure(true)
fun success() = Result(true)
// NO (stdlib)
fun failure() = SuccessOrFailure(false)
fun failure() = Result(false)
// NO (stdlib)
fun <T> runCatching(block: () -> T) = SuccessOrFailure(block())
fun <T> runCatching(block: () -> T) = Result(block())
// NO (stdlib)
fun <T> SuccessOrFailure<T>.id() = this
fun <T> Result<T>.id() = this
class ClassWithExtension() {
// NO (not SuccessOrFailure)
// NO (not Result)
fun calc() = 12345
// NO (not SuccessOrFailure)
// NO (not Result)
fun calcComplex(arg1: Int, arg2: Double): Double = arg1 + arg2
// YES (different parameters)
fun calcComplexCatching() = SuccessOrFailure(0.0)
fun calcComplexCatching() = Result(0.0)
}
// NO (extension to calc)
fun ClassWithExtension.calcCatching() = SuccessOrFailure(calc())
// NO (not SuccessOrFailure)
fun ClassWithExtension.calcCatching() = Result(calc())
// NO (not Result)
fun Container.extensionAct() = 42
// YES (different extension receiver)
fun ClassWithExtension.extensionActCatching() = SuccessOrFailure(42)
fun ClassWithExtension.extensionActCatching() = Result(42)