Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an inline constructor in Kotlin?

First of all, I have to clarify I'm not asking what's an inline function is or what's an inline class is. There's nowhere in Kotlin Language documentation or specification any reference to an inline constructor but if you look in Arrays.kt source you see this class: ByteArray has an inline constructor:

/**
 * An array of bytes. When targeting the JVM, instances of this class are represented as `byte[]`.
 * @constructor Creates a new array of the specified [size], with all elements initialized to zero.
 */
public class ByteArray(size: Int) {
    /**
     * Creates a new array of the specified [size], where each element is calculated by calling the specified
     * [init] function.
     *
     * The function [init] is called for each array element sequentially starting from the first one.
     * It should return the value for an array element given its index.
     */
    public inline constructor(size: Int, init: (Int) -> Byte)

Let's consider we want to create a similar class, something like this:

    public class Student(name: String) {
        public inline constructor(name: String, age: Int) : this(name)
    }

If you try to create that class in Kotlin and write an inline constructor for it you see that it's impossible and IDE refers to this error:

Modifier 'inline' is not applicable to 'constructor'

So let's recap, how ByteArray definition is correct?

like image 728
YaMiN Avatar asked May 03 '21 18:05

YaMiN


People also ask

What does inline mean in Kotlin?

An Inline function is a kind of function that is declared with the keyword "inline" just before the function declaration. Once a function is declared inline, the compiler does not allocate any memory for this function, instead the compiler copies the piece of code virtually at the calling place at runtime.

What is inline variable Kotlin?

An inline function is declare with a keyword inline. The use of inline function enhances the performance of higher order function. The inline function tells the compiler to copy parameters and functions to the call site. The virtual function or local function cannot be declared as inline.

How do I create an inline function in Kotlin?

To make the compiler do this, mark the lock() function with the inline modifier: inline fun <T> lock(lock: Lock, body: () -> T): T { ... } The inline modifier affects both the function itself and the lambdas passed to it: all of those will be inlined into the call site. Inlining may cause the generated code to grow.


1 Answers

The ByteArray declaration you are looking at is not real, it’s a so called built-in type. This declaration exists for convenience, but is never truly compiled to a binary. (Indeed, on the JVM, arrays are special and don’t have corresponding class files anywhere.)

This constructor is marked inline because in practice the compiler emits the code corresponding to what would be its body at every call site. All the call-site checking is done accordingly (the lambda argument is treated in such a way that the compiler knows it’s going to be inclined).

Constructor inlining is not possible for user classes, so the inline modifier is prohibited in user code.

like image 116
Andrey Breslav Avatar answered Oct 01 '22 05:10

Andrey Breslav