1
1
package com.github.michaelbull.result
2
2
3
+ import kotlin.jvm.JvmInline
4
+
3
5
/* *
4
6
* Returns a [Result] that [is ok][Result.isOk] and contains a [value][Result.value].
5
7
*/
6
- @Suppress(" FunctionName" , " DEPRECATION " )
8
+ @Suppress(" FunctionName" )
7
9
public fun <V > Ok (value : V ): Result <V , Nothing > {
8
- return Ok (value, null )
10
+ return Result (value)
9
11
}
10
12
11
13
/* *
12
14
* Returns a [Result] that [is an error][Result.isErr] and contains an [error][Result.error].
13
15
*/
14
- @Suppress(" FunctionName" , " DEPRECATION " )
16
+ @Suppress(" FunctionName" )
15
17
public fun <E > Err (error : E ): Result <Nothing , E > {
16
- return Err ( error, null )
18
+ return Result ( Failure ( error) )
17
19
}
18
20
19
21
/* *
@@ -37,94 +39,66 @@ public inline fun <V, E, F> Result<V, E>.asErr(): Result<Nothing, F> {
37
39
/* *
38
40
* [Result] is a type that represents either success ([Ok]) or failure ([Err]).
39
41
*
42
+ * A [Result] that [is ok][Result.isOk] will have a [value][Result.value] of type [V], whereas a
43
+ * [Result] that [is an error][Result.isErr] will have an [error][Result.error] of type [E].
44
+ *
40
45
* - Elm: [Result](http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Result)
41
46
* - Haskell: [Data.Either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html)
42
47
* - Rust: [Result](https://doc.rust-lang.org/std/result/enum.Result.html)
43
48
*/
44
- public sealed class Result <out V , out E > {
49
+ @JvmInline
50
+ public value class Result <out V , out E > internal constructor(
51
+ private val inlineValue : Any? ,
52
+ ) {
45
53
46
- public abstract val value: V
47
- public abstract val error: E
54
+ @Suppress(" UNCHECKED_CAST" )
55
+ public val value: V
56
+ get() = inlineValue as V
48
57
49
- public abstract val isOk: Boolean
50
- public abstract val isErr: Boolean
51
-
52
- public abstract operator fun component1 (): V ?
53
- public abstract operator fun component2 (): E ?
54
- }
58
+ @Suppress(" UNCHECKED_CAST" )
59
+ public val error: E
60
+ get() = (inlineValue as Failure <E >).error
55
61
56
- /* *
57
- * Represents a successful [Result], containing a [value].
58
- */
59
- @Deprecated(
60
- message = " Using Ok as a return type is deprecated." ,
61
- replaceWith = ReplaceWith (" Result<V, Nothing>" ),
62
- )
63
- public class Ok <out V > internal constructor(
64
- override val value : V ,
65
- @Suppress(" UNUSED_PARAMETER" ) placeholder : Any? ,
66
- ) : Result<V, Nothing>() {
67
-
68
- override val error: Nothing
69
- get() {
70
- throw NoSuchElementException ()
71
- }
62
+ public val isOk: Boolean
63
+ get() = inlineValue !is Failure <* >
72
64
73
- override val isOk : Boolean = true
74
- override val isErr : Boolean = false
65
+ public val isErr : Boolean
66
+ get() = inlineValue is Failure < * >
75
67
76
- override fun component1 (): V = value
77
- override fun component2 (): Nothing? = null
78
-
79
- override fun equals (other : Any? ): Boolean {
80
- if (this == = other) return true
81
- if (other == null || this ::class != other::class ) return false
82
-
83
- other as Ok <* >
84
-
85
- if (value != other.value) return false
86
-
87
- return true
68
+ public operator fun component1 (): V ? {
69
+ return when {
70
+ isOk -> value
71
+ else -> null
72
+ }
88
73
}
89
74
90
- override fun hashCode (): Int = value.hashCode()
91
- override fun toString (): String = " Ok($value )"
92
- }
93
-
94
- /* *
95
- * Represents a failed [Result], containing an [error].
96
- */
97
- @Deprecated(
98
- message = " Using Err as a return type is deprecated." ,
99
- replaceWith = ReplaceWith (" Result<Nothing, E>" ),
100
- )
101
- public class Err <out E > internal constructor(
102
- override val error : E ,
103
- @Suppress(" UNUSED_PARAMETER" ) placeholder : Any? ,
104
- ) : Result<Nothing, E>() {
105
-
106
- override val value: Nothing
107
- get() {
108
- throw NoSuchElementException ()
75
+ public operator fun component2 (): E ? {
76
+ return when {
77
+ isErr -> error
78
+ else -> null
109
79
}
80
+ }
110
81
111
- override val isOk: Boolean = false
112
- override val isErr: Boolean = true
113
-
114
- override fun component1 (): Nothing? = null
115
- override fun component2 (): E = error
82
+ override fun toString (): String {
83
+ return when {
84
+ isOk -> " Ok($value )"
85
+ else -> " Err($error )"
86
+ }
87
+ }
88
+ }
116
89
90
+ private class Failure <out E >(
91
+ val error : E ,
92
+ ) {
117
93
override fun equals (other : Any? ): Boolean {
118
- if (this == = other) return true
119
- if (other == null || this ::class != other::class ) return false
120
-
121
- other as Err <* >
122
-
123
- if (error != other.error) return false
94
+ return other is Failure <* > && error == other.error
95
+ }
124
96
125
- return true
97
+ override fun hashCode (): Int {
98
+ return error.hashCode()
126
99
}
127
100
128
- override fun hashCode (): Int = error.hashCode()
129
- override fun toString (): String = " Err($error )"
101
+ override fun toString (): String {
102
+ return " Failure($error )"
103
+ }
130
104
}
0 commit comments