V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
MakHoCheung
V2EX  ›  问与答

关于 kotlin 的内联函数

  •  
  •   MakHoCheung · 2020-10-07 14:44:02 +08:00 · 815 次点击
    这是一个创建于 1268 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    3 条回复    2020-10-08 00:07:13 +08:00
    MakHoCheung
        1
    MakHoCheung  
    OP
       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
        2
    SoloCompany  
       2020-10-07 15:58:56 +08:00
    inline 函数不仅仅是面向性能优化这么简单, 最重要的差别是 inline function 允许跳出当前控制流比如堆栈而普通函数不可以

    比如最典型的 let / run 可以用在任意地方, 可以 break / return, 和你所理解的 keyword 没有任何不同, 如果不是 inline 是不可能做到的
    MakHoCheung
        3
    MakHoCheung  
    OP
       2020-10-08 00:07:13 +08:00 via iPhone
    @SoloCompany 受教了大神😂,双节快乐呀
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   946 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:30 · PVG 05:30 · LAX 14:30 · JFK 17:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.