File tree 2 files changed +9
-3
lines changed
kotlinx-coroutines-core/common
2 files changed +9
-3
lines changed Original file line number Diff line number Diff line change @@ -112,7 +112,7 @@ public suspend fun <T> Flow<T>.first(predicate: suspend (T) -> Boolean): T {
112
112
* The terminal operator that returns the first element emitted by the flow and then cancels flow's collection.
113
113
* Returns `null` if the flow was empty.
114
114
*/
115
- public suspend fun <T : Any > Flow<T>.firstOrNull (): T ? {
115
+ public suspend fun <T > Flow<T>.firstOrNull (): T ? {
116
116
var result: T ? = null
117
117
collectWhile {
118
118
result = it
@@ -122,10 +122,10 @@ public suspend fun <T : Any> Flow<T>.firstOrNull(): T? {
122
122
}
123
123
124
124
/* *
125
- * The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
125
+ * The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
126
126
* Returns `null` if the flow did not contain an element matching the [predicate].
127
127
*/
128
- public suspend fun <T : Any > Flow<T>.firstOrNull (predicate : suspend (T ) -> Boolean ): T ? {
128
+ public suspend fun <T > Flow<T>.firstOrNull (predicate : suspend (T ) -> Boolean ): T ? {
129
129
var result: T ? = null
130
130
collectWhile {
131
131
if (predicate(it)) {
Original file line number Diff line number Diff line change @@ -128,6 +128,12 @@ class FirstTest : TestBase() {
128
128
assertNull(emptyFlow<Int >().firstOrNull { true })
129
129
}
130
130
131
+ @Test
132
+ fun testFirstOrNullWithNullElement () = runTest {
133
+ assertNull(flowOf<String ?>(null ).firstOrNull())
134
+ assertNull(flowOf<String ?>(null ).firstOrNull { true })
135
+ }
136
+
131
137
@Test
132
138
fun testFirstOrNullWhenErrorCancelsUpstream () = runTest {
133
139
val latch = Channel <Unit >()
You can’t perform that action at this time.
0 commit comments