有 kotlin 大佬吗。由于 kotlin 要兼容 java6,无法用到 invokedynamic,所以才会有 inline 函数的存在,但是如果我的编译目标是 java8 以上的话,kotlin 编译器会优化成 invokedynamic 吗,这样就不用写 inline 函数了
1
MakHoCheung OP 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 函数 |
2
SoloCompany 2020-10-07 15:58:56 +08:00
inline 函数不仅仅是面向性能优化这么简单, 最重要的差别是 inline function 允许跳出当前控制流比如堆栈而普通函数不可以
比如最典型的 let / run 可以用在任意地方, 可以 break / return, 和你所理解的 keyword 没有任何不同, 如果不是 inline 是不可能做到的 |
3
MakHoCheung OP @SoloCompany 受教了大神😂,双节快乐呀
|