Add 'suspendWithCurrentContinuation' and 'Suspend' object in built-ins

They are part of the new suspension convention, relevant
support in backends will be added in later commits

 #KT-14924 In Progress
This commit is contained in:
Denis Zharkov
2016-11-24 16:25:32 +03:00
parent a34c9c2580
commit 2c3b0aeddb
8 changed files with 113 additions and 1 deletions
@@ -0,0 +1,16 @@
package-fragment kotlin.coroutines
@kotlin.SinceKotlin(version = "1.1") public inline suspend fun </*0*/ T> suspendWithCurrentContinuation(/*0*/ body: (kotlin.coroutines.Continuation<T>) -> kotlin.Any?): T
@kotlin.SinceKotlin(version = "1.1") @kotlin.annotation.Target(allowedTargets = {AnnotationTarget.CLASS}) @kotlin.annotation.Retention(value = AnnotationRetention.BINARY) public final annotation class AllowSuspendExtensions : kotlin.Annotation {
/*primary*/ public constructor AllowSuspendExtensions()
}
@kotlin.SinceKotlin(version = "1.1") public interface Continuation</*0*/ in P> {
public abstract fun resume(/*0*/ data: P): kotlin.Unit
public abstract fun resumeWithException(/*0*/ exception: kotlin.Throwable): kotlin.Unit
}
@kotlin.SinceKotlin(version = "1.1") public object Suspend {
/*primary*/ private constructor Suspend()
}
@@ -0,0 +1,31 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !CHECK_TYPE
class Controller {
suspend fun noParams(): Unit = suspendWithCurrentContinuation {
if (hashCode() % 2 == 0) {
it.resume(Unit)
Suspend
}
else {
Unit
}
}
suspend fun yieldString(value: String) = suspendWithCurrentContinuation<Int> {
it.resume(1)
it checkType { _<Continuation<Int>>() }
it.resume(<!TYPE_MISMATCH!>""<!>)
// We can return anything here, 'suspendWithCurrentContinuation' is not very type-safe
// Also we can call resume and then return the value too, but it's still just our problem
"Not-int"
}
}
fun builder(coroutine c: Controller.() -> Continuation<Unit>) {}
fun test() {
builder {
noParams() checkType { _<Unit>() }
yieldString("abc") checkType { _<Int>() }
}
}
@@ -0,0 +1,13 @@
package
public fun builder(/*0*/ coroutine c: Controller.() -> kotlin.coroutines.Continuation<kotlin.Unit>): kotlin.Unit
public fun test(): kotlin.Unit
public final class Controller {
public constructor Controller()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final suspend fun noParams(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public final suspend fun yieldString(/*0*/ value: kotlin.String): kotlin.Int
}