简体   繁体   中英

Kotlin: How to create a higher order function that works inside suspend and non-suspend functions? (other than inline)

So basically, I want to create a high order function like run , that works inside non-functions as it works in suspend functions.

What seems to make it work is the inline modifier, but I'm sure that comes with some sort of overhead, so I'm wondering if there's another way.

I believe inline is the only way to achieve this. It is also a commonly used solution, both in stdlib and 3rd party code.

Regarding the overhead: it is actually the opposite. Higher-order functions are better to be inlined anyway - because of the performance. Drawback is the increased size of the resulting bytecode, so slightly increased memory usage. However, this shouldn't be a concern as long as the inlined function isn't really big.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM