DEV Community

Oğuz Şahin
Oğuz Şahin

Posted on

All About Kotlin Inline(Value) Class

Image description

Hello, All ✋. In this article, we will try to learn everything about Kotlin Inline Class in detail.


What Is The Inline Class❓

In some cases, we create wrappers to provide certain business logic. As such, these additional classes also cause a performance loss when they create additional loads during runtime. If it is a primitive type you wrap, the usage here can become terrible in terms of performance. To solve this situation, the kotlin team introduced a new class named Inline Class.

In this case, the inline class not only provides type safety by carrying a class feature but also acts as a primitive type on the side of the compiled code and saves unnecessary performance loss. It also offers a much cleaner, understandable and readable code structure.


Why is there an extra performance loss? 🤔

from https://www.javatpoint.com/stack-vs-heap-java

When we create a primitive type variable, it is stored in the stack of the JVM memory, but when we create a new object, it is stored in a region called a heap. A more expensive operation is going around the back when storing and using objects. You can check the link to go into more detail about this heap and stack event. The losses we are talking about are immeasurably small. But if we consider the job of creating an object created in a whole code, it is obvious that it will create an extra overhead. In addition, as programmers, should not forget that we need to write the code more cleanly, clearly, and with performance. But when a comparison is made, defining a primitive type is more performance efficient than defining an object. For this reason, if we are writing a wrapper class for a primitive type, by choosing the Inline class, we will not only benefit from the benefits of the wrapper class such as more flexible, readable, and type safety, but also we will not experience performance loss by acting as a primitive type on the compiled code side of the inline class.

from https://quickbirdstudios.com/blog/kotlin-value-classes/

In addition, there is a comparison of how long the process will cost us when the same process is done in 4 different ways.

Usage and Limits
Inline Class Definition

🔹 The value keyword is sufficient to define an inline class.

🔹 If you are writing a kotlin code for the JVM, we also need to add the @JvmInline annotation.

Image description

🔹 We can use a single primary constructor.

🔹 It is mandatory to define a property such as data class.

🔹 We can define only one read-only property in the primary constructor (var is not accepted. Only Val keyword)

Image description

🔹 We can define member read-only properties and their values ​​can be from singletons, top-level objects, or constants (not allowed in backing file).

🔹 The init block can be used here as well as in a regular class.

🔹 Member functions can be defined.

Image description

🔹 They cannot inherit from any class or any class cannot inherit from an inline class.

🔹 They can implement an interface.


Mangling
Mangling

There are two functions in the above attachment. One of them takes a parameter of type value class, and the other takes a parameter of type int. There is a little change in the code on the side where this code is compiled. As you can see, the value class is first compiled as a primitive type. This can lead to certain errors. For example, consider the two functions above. Here, there will be a conflict as the distance type will be int type on the JVM side when compiling. To avoid these errors, the function name is shuffled when compiling the inline class. When you look at the compiled code on the right, it can be seen that it is compiled as getPrice.

If we want to call the first function from java, we need to disable the mangling event in it. We can do this by adding the “@JvmName” annotation. By adding this annotation, the name argument we give will be the function name in the compiled code. We can also call from java classes over this function name.

Image description


Type Alias vs Inline Class

We can say that the use of inline class is very similar to the use of type alias. But there are also some differences. Although both seem to create a new type, what we do with type alias is to replace the name of the type. For example, we can think of assigning a different name for the String type. In the use of Inline class, we create a new type. This provides us with a clearer and more understandable code structure, as well as type safety.

Image description


Conclusion

In this article, we tried to learn every detail about the Inline class. The use of inline class promises us good things in terms of writing the code more cleanly and legibly, with the advantage it provides in terms of performance and the type safety it provides.
See you in the next articles 👋👋.


References :
https://kotlinlang.org/docs/inline-classes.html
https://www.geeksforgeeks.org/kotlin-inline-classes/
https://typealias.com/guides/introduction-to-inline-classes/
https://quickbirdstudios.com/blog/kotlin-value-classes

Top comments (0)