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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值