Skip to content

Allow nullable types in Flow's firstOrNull extension #2230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions kotlinx-coroutines-core/common/src/flow/terminal/Reduce.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public suspend fun <T> Flow<T>.single(): T {
* The terminal operator, that awaits for one and only one value to be published.
* Throws [IllegalStateException] for flow that contains more than one element.
*/
public suspend fun <T: Any> Flow<T>.singleOrNull(): T? {
public suspend fun <T> Flow<T>.singleOrNull(): T? {
var result: T? = null
collect { value ->
if (result != null) error("Expected only one element")
Expand Down Expand Up @@ -124,7 +124,7 @@ public suspend fun <T> Flow<T>.first(predicate: suspend (T) -> Boolean): T {
* The terminal operator that returns the first element emitted by the flow and then cancels flow's collection.
* Returns `null` if the flow was empty.
*/
public suspend fun <T : Any> Flow<T>.firstOrNull(): T? {
public suspend fun <T> Flow<T>.firstOrNull(): T? {
var result: T? = null
try {
collect { value ->
Expand All @@ -138,10 +138,10 @@ public suspend fun <T : Any> Flow<T>.firstOrNull(): T? {
}

/**
* The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
* The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
* Returns `null` if the flow did not contain an element matching the [predicate].
*/
public suspend fun <T : Any> Flow<T>.firstOrNull(predicate: suspend (T) -> Boolean): T? {
public suspend fun <T> Flow<T>.firstOrNull(predicate: suspend (T) -> Boolean): T? {
var result: T? = null
try {
collect { value ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ class FirstTest : TestBase() {
assertNull(emptyFlow<Int>().firstOrNull { true })
}

@Test
fun testFirstOrNullWithNullElement() = runTest {
assertNull(flowOf<String?>(null).firstOrNull())
assertNull(flowOf<String?>(null).firstOrNull { true })
}

@Test
fun testFirstOrNullWhenErrorCancelsUpstream() = runTest {
val latch = Channel<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package kotlinx.coroutines.flow
import kotlinx.coroutines.*
import kotlin.test.*

class SingleTest : TestBase() {
class SingleTest : TestBase() {

@Test
fun testSingle() = runTest {
Expand Down Expand Up @@ -61,6 +61,10 @@ class SingleTest : TestBase() {
assertEquals(1, flowOf<Int?>(1).single())
assertNull(flowOf<Int?>(null).single())
assertFailsWith<NoSuchElementException> { flowOf<Int?>().single() }

assertEquals(1, flowOf<Int?>(1).singleOrNull())
assertNull(flowOf<Int?>(null).singleOrNull())
assertNull(flowOf<Int?>().singleOrNull())
}

@Test
Expand Down