关于 kotlin 的内联函数

2020-10-07 14:44:02 +08:00
 MakHoCheung

有 kotlin 大佬吗。由于 kotlin 要兼容 java6,无法用到 invokedynamic,所以才会有 inline 函数的存在,但是如果我的编译目标是 java8 以上的话,kotlin 编译器会优化成 invokedynamic 吗,这样就不用写 inline 函数了

837 次点击
所在节点    问与答
3 条回复
MakHoCheung
2020-10-07 15:22:50 +08:00
You never need to use inline functions. (I believe) they are purely there for free performance optimizations to prevent extra object creation at runtime. It achieves this by essentially turning the function into a macro and copying that function wherever it's used. Generally there's no point unless it's a higher order function. (And intelij will tell you this if you use it without need). Although I wouldn't call myself super experienced with kotlin so I could just be spewing garbage.

明明都说了有性能优化,老外的回答居然说不需要 inline 函数
SoloCompany
2020-10-07 15:58:56 +08:00
inline 函数不仅仅是面向性能优化这么简单, 最重要的差别是 inline function 允许跳出当前控制流比如堆栈而普通函数不可以

比如最典型的 let / run 可以用在任意地方, 可以 break / return, 和你所理解的 keyword 没有任何不同, 如果不是 inline 是不可能做到的
MakHoCheung
2020-10-08 00:07:13 +08:00
@SoloCompany 受教了大神😂,双节快乐呀

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/712908

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX