Files
kotlin-fork/compiler/testData/codegen/boxInline/suspend/enclodingMethod.kt
T
Ivan Kylchik c7435ba760 Replace all occurrences of WITH_RUNTIME with WITH_STDLIB
We are going to deprecate `WITH_RUNTIME` directive. The main reason
behind this change is that `WITH_STDLIB` directive better describes
its meaning, specifically it will add kotlin stdlib to test's classpath.
2021-11-17 15:26:38 +03:00

57 lines
1.5 KiB
Kotlin
Vendored

// WITH_COROUTINES
// WITH_STDLIB
// FULL_JDK
// TARGET_BACKEND: JVM
// FILE: flow.kt
package flow
interface FlowCollector<T> {
suspend fun emit(value: T)
}
interface Flow<T> {
suspend fun collect(collector: FlowCollector<T>)
}
public inline fun <T> flow(crossinline block: suspend FlowCollector<T>.() -> Unit) = object : Flow<T> {
override suspend fun collect(collector: FlowCollector<T>) = collector.block()
}
suspend inline fun <T> Flow<T>.collect(crossinline action: suspend (T) -> Unit): Unit =
collect(object : FlowCollector<T> {
override suspend fun emit(value: T) = action(value)
})
public inline fun <T, R> Flow<T>.transform(crossinline transformer: suspend FlowCollector<R>.(value: T) -> Unit): Flow<R> {
return flow {
collect { value ->
transformer(value)
}
}
}
public inline fun <T, R> Flow<T>.map(crossinline transformer: suspend (value: T) -> R): Flow<R> = transform { value -> emit(transformer(value)) }
inline fun decorate() = suspend {
flow<Int> {
emit(1)
}.map { it + 1 }
.collect {
}
}
// FILE: box.kt
import flow.*
fun box() : String {
decorate()
val enclosingMethod = try {
Class.forName("flow.FlowKt\$decorate\$1\$invokeSuspend\$\$inlined\$map\$1\$1").enclosingMethod
} catch (ignore: ClassNotFoundException) {
Class.forName("flow.FlowKt\$decorate\$1\$doResume\$\$inlined\$map\$1\$1").enclosingMethod
}
if ("$enclosingMethod".contains("\$\$forInline")) return "$enclosingMethod"
return "OK"
}