Comments (3)
我在知乎上看到这么一段话
Kotlin 并不建议将非高阶函数定义为内联函数:
inline fun foo() = println("foo")编译,Kotlin 编译器会给我们一个警告:
Warning:(1, 1) Kotlin: Expected performance impact of inlining 'public inline fun foo(): Unit defined in root package' can be insignificant. Inlining works best for functions with lambda parameters
编译器告诉我们,内联最好与有 Lambda 表达式参数的函数配合使用,内联这里的 foo() 函数对性能的影响微乎其微。
from ktkit.
Inline (内联函数) 的作用:提升运行效率,调用被 inline 修饰符的函数,会把里面的代码放到我调用的地方。
Inline 修饰符适用于下面两种情况,编译器才不会有警告
- 将函数类型作为参数
- 结合 reified 实化类型参数参数
很多扩展方法代码段很短,使用 Inline 修饰符, 将方法体的代码段放到调用处,来提升性能(虽然微乎其微),但是编译器会给出警告,所以添加 InlineOnly 注解有以下作用
- 消除警告
- 修改内联函数的可见性,默认是public, 加上修饰符,编译时改成 private
from ktkit.
学习了
from ktkit.
Related Issues (7)
- ./gradlew spotlessApply 命令运行不了 HOT 2
- 关于view的操作的bug HOT 2
- CharSequence和String的问题 HOT 2
- startActivity HOT 3
- View.clickTrigger的BUG HOT 1
- 跟DataStore有冲突 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ktkit.