Kotlin 中的inline, noinline与crossinline

其中的inline比较好理解,就是在编译时把调用这个函数进行替换,而noinline依据字面意思也比较好理解。但这个inline与noinline结合又有一个非局部返回(Non-local returns)出现。且看下图中的官方解释




crossinline 的作用是让被标记的lambda表达式不允许非局部返回。

首先,默认内联函数的lambda表达式参数是允许非局部返回的,下面是官方的解释。

Note that some inline functions may call the lambdas passed to them as parameters not directly from the function body, but from another execution context, such as a local object or a nested function. In such cases, non-local control flow is also not allowed in the lambdas. To indicate that, the lambda parameter needs to be marked with the crossinline modifier:

也就是说加了crossinline 的函数如果直接将lambda参数作为另一个函数的返回值,这种情况是不被允许的。



下附参考链接

http://blog.csdn.net/u013009899/article/details/78584994

http://kotlinlang.org/docs/reference/inline-functions.html


posted @ 2018-01-14 10:47  YorkYu  阅读(1683)  评论(0编辑  收藏  举报