Skip to content

Commit 4aefef4

Browse files
committed
Get rid of NULL comparison because of T: Any upper bound in firstOrNull
1 parent 8694e97 commit 4aefef4

File tree

1 file changed

+4
-5
lines changed
  • kotlinx-coroutines-core/common/src/flow/terminal

1 file changed

+4
-5
lines changed

kotlinx-coroutines-core/common/src/flow/terminal/Reduce.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public suspend fun <T: Any> Flow<T>.singleOrNull(): T? {
7777
if (result != null) error("Expected only one element")
7878
result = value
7979
}
80-
8180
return result
8281
}
8382

@@ -126,7 +125,7 @@ public suspend fun <T> Flow<T>.first(predicate: suspend (T) -> Boolean): T {
126125
* Returns `null` if the flow was empty.
127126
*/
128127
public suspend fun <T : Any> Flow<T>.firstOrNull(): T? {
129-
var result: Any? = NULL
128+
var result: Any? = null
130129
try {
131130
collect { value ->
132131
result = value
@@ -135,15 +134,15 @@ public suspend fun <T : Any> Flow<T>.firstOrNull(): T? {
135134
} catch (e: AbortFlowException) {
136135
// Do nothing
137136
}
138-
return result.takeUnless { it === NULL } as T?
137+
return result.takeUnless { it === null } as T?
139138
}
140139

141140
/**
142141
* The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
143142
* Returns `null` if the flow did not contain an element matching the [predicate].
144143
*/
145144
public suspend fun <T : Any> Flow<T>.firstOrNull(predicate: suspend (T) -> Boolean): T? {
146-
var result: Any? = NULL
145+
var result: Any? = null
147146
try {
148147
collect { value ->
149148
if (predicate(value)) {
@@ -154,5 +153,5 @@ public suspend fun <T : Any> Flow<T>.firstOrNull(predicate: suspend (T) -> Boole
154153
} catch (e: AbortFlowException) {
155154
// Do nothing
156155
}
157-
return result.takeUnless { it === NULL } as T?
156+
return result.takeUnless { it === null } as T?
158157
}

0 commit comments

Comments
 (0)