Sobes.tech
Middle

When does overload occur when using inline functions?

sobes.tech AI

Answer from AI

Overloading inline functions in Kotlin does not occur in the traditional sense, as with regular functions. In fact, an inline function at call sites replaces itself with its body. This means that during JVM bytecode compilation, there is no separate function call, and instead, code from the function body is inserted.

Here are the main points regarding "overloading" and inline functions:

  • No direct call: An inline function does not generate a separate method in bytecode that can be overloaded.
  • Code substitution: The body of the inline function is directly inserted at its call sites.
  • Impact of generic parameters: If an inline function has generic parameters, the compiler may create specialized versions of the code for different specific types during substitution. This is similar to specialization in C++, but it is not classical overloading.
  • Impact of higher-order functions: Inline functions are often used with higher-order functions. If a lambda expression passed to an inline function is also inlined, then the bytecode of the lambda is also inserted directly. If the lambda is not inlined (noinline), then a separate object and method are created for it.

Thus, "overloading" of inline functions does not occur at the method signature level but at the compilation and code substitution level, where the compiler can generate slightly different code fragments depending on the call context (for example, for generic parameters). This is not classical polymorphic overloading.