Kotlin协程的@RestrictsSuspension注解

Classes and interfaces marked with this annotation are restricted when used as receivers for >extension
suspend functions. These suspend extensions can only invoke other member or extension >suspend functions on this particular
receiver and are restricted from calling arbitrary suspension functions

简单来说,如果一个接口或者类被标注@RestrictsSuspension,那么当这个类作为suspend扩展函数接受者的时候,其suspend函数内部不能调用其它CoroutineScope的suspend函数。

suspend fun SequenceScope<String>.test(block: suspend GlobalScope.() -> Int){
    block()
}

报错:Restricted suspending functions can only invoke member or extension 
suspending functions on their restricted coroutine scope

上面的代码,在SequenceScope的test扩展函数中调用了GlobalScope的扩展lambda--block;而SequenceScope正是被@RestrictsSuspension注解修饰。所以编译器会报错!

@RestrictsSuspension
@SinceKotlin("1.3")
public abstract class SequenceScope<in T> internal constructor(){。。。}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 官网 中文版本 好的网站 Content-type: text/htmlBASH Section: User ...
    不排版阅读 4,537评论 0 5
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,554评论 0 13
  • 函数调用 Built-in Functions abs(x) Return the absolute value ...
    叫我七夜阅读 1,226评论 0 0
  • 日色欲尽,将暮未暮的天空晕染上一层浓重的黑色。 曲凉州一个人坐在天桥边,微微吹拂的夜风拂过这张布满死寂的脸,她的眼...
    南风起兮昔人归阅读 298评论 0 0
  • 各位亲爱的瑜舍家人们, 昨天和一位离开又想再回来的伙伴聊了很久,他一直非常认可瑜舍,他问我这两年瑜伽馆雨后春笋般地...
    瑾爷Jaya阅读 636评论 2 8