@@ -29,7 +29,8 @@ import kotlinx.coroutines.flow.unsafeFlow as flow
29
29
public inline fun <T , R > Flow<T>.transform (@BuilderInference crossinline transform : suspend FlowCollector <R >.(value: T ) -> Unit ): Flow <R > {
30
30
return flow {
31
31
collect { value ->
32
- transform(value)
32
+ // kludge, without it Unit will be returned and TCE won't kick in, KT-28938
33
+ return @collect transform(value)
33
34
}
34
35
}
35
36
}
@@ -40,7 +41,7 @@ public inline fun <T, R> Flow<T>.transform(@BuilderInference crossinline transfo
40
41
@FlowPreview
41
42
public inline fun <T > Flow<T>.filter (crossinline predicate : suspend (T ) -> Boolean ): Flow <T > = flow {
42
43
collect { value ->
43
- if (predicate(value)) emit(value)
44
+ if (predicate(value)) return @collect emit(value)
44
45
}
45
46
}
46
47
@@ -50,7 +51,7 @@ public inline fun <T> Flow<T>.filter(crossinline predicate: suspend (T) -> Boole
50
51
@FlowPreview
51
52
public inline fun <T > Flow<T>.filterNot (crossinline predicate : suspend (T ) -> Boolean ): Flow <T > = flow {
52
53
collect { value ->
53
- if (! predicate(value)) emit(value)
54
+ if (! predicate(value)) return @collect emit(value)
54
55
}
55
56
}
56
57
@@ -66,15 +67,15 @@ public inline fun <reified R> Flow<*>.filterIsInstance(): Flow<R> = filter { it
66
67
*/
67
68
@FlowPreview
68
69
public fun <T : Any > Flow<T?>.filterNotNull (): Flow <T > = flow<T > {
69
- collect { value -> if (value != null ) emit(value) }
70
+ collect { value -> if (value != null ) return @collect emit(value) }
70
71
}
71
72
72
73
/* *
73
74
* Returns a flow containing the results of applying the given [transform] function to each value of the original flow.
74
75
*/
75
76
@FlowPreview
76
77
public inline fun <T , R > Flow<T>.map (crossinline transform : suspend (value: T ) -> R ): Flow <R > = transform { value ->
77
- emit(transform(value))
78
+ return @transform emit(transform(value))
78
79
}
79
80
80
81
/* *
@@ -83,7 +84,7 @@ public inline fun <T, R> Flow<T>.map(crossinline transform: suspend (value: T) -
83
84
@FlowPreview
84
85
public inline fun <T , R : Any > Flow<T>.mapNotNull (crossinline transform : suspend (value: T ) -> R ? ): Flow <R > = transform { value ->
85
86
val transformed = transform(value) ? : return @transform
86
- emit(transformed)
87
+ return @transform emit(transformed)
87
88
}
88
89
89
90
/* *
0 commit comments