Is there guidance as to when a property should be ...
# getting-started
e
Is there guidance as to when a property should be inlined (aside from needing reified parameter types)?
l
The IDE will tell you if something shouldn’t be inlined, so I generally lean towards marking as inline and changing back if the IDE says to.
g
TL;DR IDEA says that inling should be applied only when there is inlined lambda argument or there is reified type parameter. But actually you should rely on common sense instead of IDE. As I understand the following is true. 1. There are 2 common approaches to use inlining: inlining lambdas (thus, removing creation of Function object, that is unnecessarily slow and allocates extra memory for the object each time) and reified types. 2. Kotlin compiler warnings about "unnecessary inlining" appear when (and only when) the compiler does not recognize any of the cases above, i.e. if there is either no lambda argument to inline (or all lambda arguments are marked with
noinline
, which is the same) nor type parameter to reify. 3. All IntelliJ IDEA warnings about inling are inherited from Kotlin compiler. (Don't know about other IDEs' warnings.) Also there is advise not to apply inlining to functions with large bodies (otherwise, their large bodies will be inlined in every place they are used in, so size of generated bytecode will increase rapidly). All of the three advises should be written in docs. But as I can see, it is a bit implicit. But it's described very good in book Kotlin in Action. Also there is case of common sense. For example, if you have very simple function like
Copy code
fun process(a: A, b: Int) = TODO()
fun A.process() = process(this, 15)
then it's better to inline seccomd function, and the code will be a very bit faster, because there are less links to use.