// TARGET_BACKEND: JVM // FILE: flow.kt // COMMON_COROUTINES_TEST // FULL_JDK // WITH_RUNTIME // WITH_COROUTINES package flow interface FlowCollector { suspend fun emit(value: T) } interface Flow { suspend fun collect(collector: FlowCollector) } public inline fun flow(crossinline block: suspend FlowCollector.() -> Unit) = object : Flow { override suspend fun collect(collector: FlowCollector) = collector.block() } suspend inline fun Flow.collect(crossinline action: suspend (T) -> Unit): Unit = collect(object : FlowCollector { override suspend fun emit(value: T) = action(value) }) public inline fun Flow.transform(crossinline transformer: suspend FlowCollector.(value: T) -> Unit): Flow { return flow { collect { value -> transformer(value) } } } public inline fun Flow.map(crossinline transformer: suspend (value: T) -> R): Flow = transform { value -> emit(transformer(value)) } inline fun decorate() = suspend { flow { emit(1) }.map { it + 1 } .collect { } } // FILE: box.kt // COMMON_COROUTINES_TEST 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" }