在界面中声明最终乐趣

时间:2018-01-12 19:48:53

标签: kotlin coroutine

我正在Kotlin写一个合作多线程引擎。我正在尝试编写如下界面:

interface Processor {
    var suspendAction: (Continuation<Any>) -> Unit

    inline suspend fun yield() = suspendCoroutine(suspendAction)

    suspend fun process(inbox: Inbox) = Unit
}

yield()是我想要为此接口的所有实现者提供的服务。由于每个虚拟呼叫站点都代表内联的障碍,并且由于每个入口点suspend fun都有其成本,出于性能原因,我需要此函数为final,但Kotlin不允许我这样做。我找到了将yield()变为扩展乐趣的解决方法:

inline suspend fun Processor.yield() = suspendCoroutine(suspendAction)

我想问一下,用例是否可能会激励Kotlin语言设计师在final fun中允许interface

请注意,与典型的等待IO暂停方案不同,此处yield()出现在热CPU密集型线程上。

0 个答案:

没有答案
相关问题