Skip to content

Commit c901bee

Browse files
committed
Remove the type constraint for Flow.singleOrNull
1 parent 1de6bf0 commit c901bee

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public suspend fun <T> Flow<T>.single(): T {
7171
* The terminal operator, that awaits for one and only one value to be published.
7272
* Throws [IllegalStateException] for flow that contains more than one element.
7373
*/
74-
public suspend fun <T: Any> Flow<T>.singleOrNull(): T? {
74+
public suspend fun <T> Flow<T>.singleOrNull(): T? {
7575
var result: T? = null
7676
collect { value ->
7777
if (result != null) error("Expected only one element")

kotlinx-coroutines-core/common/test/flow/terminal/SingleTest.kt

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class SingleTest : TestBase() {
6161
assertEquals(1, flowOf<Int?>(1).single())
6262
assertNull(flowOf<Int?>(null).single())
6363
assertFailsWith<NoSuchElementException> { flowOf<Int?>().single() }
64+
65+
assertEquals(1, flowOf<Int?>(1).singleOrNull())
66+
assertNull(flowOf<Int?>(null).singleOrNull())
67+
assertFailsWith<NoSuchElementException> { flowOf<Int?>().singleOrNull() }
6468
}
6569

6670
@Test

0 commit comments

Comments
 (0)