Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin introduced inline classes with version 1.3 as an experimental feature. You should be aware that their implementation can still change in future releases, but it’s already a great time to learn about them now. Inline classes add a simple tool we can use to wrap some other type without adding runtime overhead through additional heap allocations. In this article, we want to see how inline classes in Kotlin work and when it makes sense to use them.

Enable inline classes in your project

To enable inline classes in your project, you simply need to work with Kotlin version > 1.3 which adds the

inline

keyword to the language. Since inline classes are still experimental, your IDE will display related warnings when you make use of them. The warnings can be disabled with explicit compiler flags as described here.

The first glance at inline classes

Inline classes are pretty straightforward. To make a class inlined, you just add the

inline

keyword to your class:


inline class WrappedInt(val value: Int)

Inline classes have some, more or less apparent, restrictions though: It’s required to specify precisely one property in the primary constructor, as shown with

value

. You can’t wrap multiple values in one

inline

class. It is also prohibited to have

init

blocks in inline classes, and you cannot have properties with backing fields. Inline classes can have simple computable properties, however, which we will see later in this article.

At runtime, the wrapped type of an inline class will be used without its wrapper whenever possible. This is similar to Java’s boxed types like

Integer

or

Boolean

, which will be represented as their corresponding primitive type whenever the compiler can do that. That exactly is the great selling point for inline classes in Kotlin: When you inline a class, the class itself won’t be used in the byte code unless it’s absolutely necessary. Inlining classes drastically reduces space overhead at runtime.

Runtime representation

At runtime, an inline class can be represented as both, the wrapper type and the underlying type. As mentioned in the previous paragraph, the compiler prefers using the underlying (wrapped) type of an

inline

class to optimize the code as much as possible. This is similar to boxing between

int

and

Integer

. In certain situations, however, the compiler needs to use the wrapper itself, so it will be generated during compilation:


public final class WrappedInt {
   private final int value;

   public final int getValue() { return this.value; }

   // $FF: synthetic method
   private WrappedInt(int value) { this.value = value; }

   public static int constructor_impl(int value) { return value; }

   // $FF: synthetic method
   @NotNull
   public static final WrappedInt box_impl(int v) { return new WrappedInt(v); }

   // $FF: synthetic method
   public final int unbox_impl() { return this.value; }

   //more Object related implementations
}

This snippet shows the simplified byte code represented as Java code to show how an

inline

class looks like. Along with some obvious stuff like the

value

field and its getter, the constructor is private, and new objects will rather be created through

constructor_impl

which does not actually use the wrapper type but only returns the passed in underlying type. Finally, you can see

box_impl

and

unbox_impl

functions which, as you might expect, are used for boxing purposes. Now let’s see how this inline class wrapper gets utilized when we use the

inline

class in our code.

Using inline classes


fun take(w: WrappedInt) {
    println(w.value)
}

fun main() {
    val inlined = WrappedInt(5)
    take(inlined)
}

In this snippet, a

WrappedInt

is being created and passed to a function which prints its wrapped value. The corresponding byte code, again as Java code, looks as follows:


public static final void take_hqTGqkw(int w) {
    System.out.println(w);
}

public static final void main() {
    int inlined = WrappedInt.constructor_impl(5);
    take_hqTGqkw(inlined);
}

In the compiled code, no instance of

WrappedInt

is created. Although the static

constructor_impl

is used, it just returns an

int

which is then passed to the

take

function that also does not know anything about the type of the

inline

class which we originally had in our source code.
Note that the names of functions accepting inline class parameters are extended with a generated hash code in the byte code. This way, they can stay distinguishable from overloaded functions accepting the underlying type as a parameter:


fun take(w: WrappedInt) = println(w.value)
fun take(v: Int) = println(v.value)

To make both

take

methods available in the JVM byte code and avoid signature clashes, the compiler renames the first one to something like

take-hqTGqkw

. Note that the example above does show a ”

_

” rather than a ”

-

” since Java does not allow method names to contain the dash which is also the reason why methods accepting inline classes are not callable from Java.

Boxing of inline classes

We saw earlier that

box_impl

and

unbox_impl

functions are created for inline classes, so when do we need them? The Kotlin docs cite a rule of thumb which says:

Inline classes are boxed whenever they are used as another type.

Boxing happens, for instance, when you use your inline class as a generic type or a nullable type:


inline class WrappedInt(val value: Int)

fun take(w: WrappedInt?) {
    if (w != null) println(w.value)
}

fun main() {
    take(WrappedInt(5))
}

In this code we modified the

take

function to take a nullable

WrappedInt

and print the underlying type if the argument is not null.


public static final void take_G1XIRLQ(@Nullable WrappedInt w) {
    if (Intrinsics.areEqual(w, (Object)null) ^ true) {
        int var1 = w.unbox_impl();
        System.out.println(var1);
    }
}

public static final void main() {
    take_G1XIRLQ(WrappedInt.box_impl(WrappedInt.constructor_impl(5)));
}

In the byte code,

take

now does not accept the underlying type directly anymore. It has to work with the wrapper type instead. When printing its content,

unbox_impl

is invoked. On the caller site, we can see that

box_impl

is used to create a boxed instance of

WrappedInt

.

It should be evident that we want to avoid boxing whenever possible. Keep in mind that specific usages of inline classes and also primitive types, in general, rely on this technique and might have to be reconsidered.

Use Cases inline classes

We saw that inline classes have a huge advantage: In the best case, they reduce runtime overhead drastically since additional heap allocations are avoided. But when do we want to use wrapper types anyway?

Better typing with inline classes

Imagine an authentication method in an API that looks as follows:


fun auth(userName: String, password: String) { println("authenticating $userName.") }

In a good world, everybody would call it with a user name and their password. However, it is not far-fetched that certain users will invoke this method differently:


auth("12345", "user1")

Since both parameters are of type

String

, you may mess up their order which gets even more likely with an increasing number of arguments of course. Wrappers around these types can help you mitigate that risk, and therefore inline classes are an awesome tool:


inline class Password(val value: String)
inline class UserName(val value: String)

fun auth(userName: UserName, password: Password) { println("authenticating $userName.")}

fun main() {
    auth(UserName("user1"), Password("12345"))
    //does not compile due to type mismatch
    auth(Password("12345"), UserName("user1"))
}

The parameter list has become less confusing and, on the caller site, the compiler will not allow a mismatch. The previously described is probably the most common scenario for using inline classes. They give you simple, type-safe wrappers without introducing additional heap allocations. For these situations, inline classes should be preferred whenever possible. Nevertheless, inline classes can be even smarter, which the next use case demonstrates.

Handling state without additional space

Let’s consider a method that takes a numeric string and parses it into a

BigDecimal

while also adjusting its scale:


/**
 * parses string number into BigDecimal with a scale of 2
 */
fun parseNumber(number: String): BigDecimal {
    return number.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun main() {
    println(parseNumber("100.12212"))
}

The code is pretty straightforward and would work just fine, but a requirement could be that you need to keep somehow track of the original string that was used to parse the number. To solve that, you would probably create a wrapper type or maybe use the existing

Pair

class to return a pair of values from that function. Those approaches would be valid although it obviously allocates additional space, which, in particular situation, should be avoided.
Inline classes can help you with that. We already noticed that inline classes can’t have multiple properties with backing fields. However, they can have simple calculated members in the form of properties and functions. We can create an inline class for our use case that wraps the original

String

and provides a method or a property that, on demand, parses our value. For the user, this will look like a normal data wrapper around two types while it does not add any runtime overhead in the best case:


inline class ParsableNumber(val original: String) {
    val parsed: BigDecimal
        get() = original.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun getParsableNumber(number: String): ParsableNumber {
    return ParsableNumber(number)
}

fun main() {
    val parsableNumber = getParsableNumber("100.12212")
    println(parsableNumber.parsed)
    println(parsableNumber.original)
}

As you can see, the

getParsableNumber

method returns an instance of our

inline

class which provides two properties

original

(the underlying type) and

parsed

(the calculated parsed number). That’s an interesting use case that is worth observing on a byte code level again:

More byte code


public final class ParsableNumber {
   @NotNull
   private final String original;

   @NotNull
   public final String getOriginal() { return this.original; }

   // $FF: synthetic method
   private ParsableNumber(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      super();
      this.original = original;
   }

   @NotNull
   public static final BigDecimal getParsed_impl(String $this) {
      BigDecimal var10000 = (new BigDecimal($this)).setScale(2, RoundingMode.HALF_UP);
      Intrinsics.checkExpressionValueIsNotNull(var10000, "original.toBigDecimal().…(2, RoundingMode.HALF_UP)");
      return var10000;
   }

   @NotNull
   public static String constructor_impl(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      return original;
   }

   // $FF: synthetic method
   @NotNull
   public static final ParsableNumber box_impl(@NotNull String v) {
      Intrinsics.checkParameterIsNotNull(v, "v");
      return new ParsableNumber(v);
   }

   // $FF: synthetic method
   @NotNull
   public final String unbox_impl() { return this.original; }

    //more Object related implementations
}

The generated wrapper class

ParsableNumber

pretty much looks like the earlier shown

WrappedInt

class. One important difference, however, is the

getParsed_impl

function, which represents our computable property

parsed

. As you can see, the function is implemented as a static function that takes a string and returns a

BigDecimal

. So how is this being utilized in the caller code?


@NotNull
public static final String getParsableNumber(@NotNull String number) {
    Intrinsics.checkParameterIsNotNull(number, "number");
    return ParsableNumber.constructor_impl(number);
}

public static final void main() {
    String parsableNumber = getParsableNumber("100.12212");
    BigDecimal var1 = ParsableNumber.getParsed_impl(parsableNumber);
    System.out.println(var1);
    System.out.println(parsableNumber);
}

As expected,

getParsableNumber

does not have any reference to our wrapper type. It simply returns the

String

without introducing any new type. In the

main

, we see that the static

getParsed_impl

is used to parse the given

String

into a

BigDecimal

. Again, no usage of

ParsableNumber

.

Reducing the scope of extension functions

A common issue with extension functions is that they may pollute your namespace if defined on general types like

String

. As an example, you may want to have an extension function that converts a JSON string into a corresponding type:


inline fun <reified T> String.asJson() = jacksonObjectMapper().readValue<T>(this)

To convert a given string into some data holder

JsonData

, you would then do:


val jsonString = """{ "x":200, "y":300 }"""
val data: JsonData = jsonString.asJson()

However, the extension function is available on strings that represent other data as well although it might not make much sense:


"whatever".asJson<JsonData> //will fail

This code will fail since the

String

does not contain valid JSON data. What can we do to make the extension shown above only available for certain strings? Yep, inline classes can help with that:

Narrow down extension scope with inline class


inline class JsonString(val value: String)
inline fun <reified T> JsonString.asJson() = jacksonObjectMapper().readValue<T>(this.value)

When we introduce a wrapper for strings that holds JSON data and change the extension to using a

JsonString

receiver accordingly, the issue described above has been solved. The extension won’t anymore appear on any arbitrary

String

but only those we consciously wrapped in a

JsonString

.

Unsigned Types

Another great use case of inline classes becomes apparent when looking at the unsigned integer types that were added to the language with version 1.3, also as an experimental feature:


public inline class UInt @PublishedApi internal constructor(@PublishedApi internal val data: Int) : Comparable<UInt>

As you can see, the

UInt

class is defined as an unsigned class that wraps a normal signed integer

data

. You can learn more about this feature in the corresponding KEEP.

Conclusion

Inline classes are a great tool that can be used to reduce heap allocations for wrapper types and helps us with solving different kinds of problems. However, be aware that certain scenarios such as using inline classes as nullable types require boxing. Since inline classes are still experimental, you have to expect your code to break in future releases due to changes in their behavior. Please keep this in mind. Nevertheless, I think it’s valid to start using them right away.

If you want to learn more about Kotlin’s beautiful features I recommend the book Kotlin in Action to you and also like to direct you to my other articles 🙂

Another great article about inline classes can be found here (by Dave Leeds).

The post Kotlin Inline Classes – How they work and when you should use them appeared first on Kotlin Expertise Blog.

Continue Reading Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin introduced inline classes with version 1.3 as an experimental feature. You should be aware that their implementation can still change in future releases, but it’s already a great time to learn about them now. Inline classes add a simple tool we can use to wrap some other type without adding runtime overhead through additional heap allocations. In this article, we want to see how inline classes in Kotlin work and when it makes sense to use them.

Enable inline classes in your project

To enable inline classes in your project, you simply need to work with Kotlin version > 1.3 which adds the

inline

keyword to the language. Since inline classes are still experimental, your IDE will display related warnings when you make use of them. The warnings can be disabled with explicit compiler flags as described here.

The first glance at inline classes

Inline classes are pretty straightforward. To make a class inlined, you just add the

inline

keyword to your class:


inline class WrappedInt(val value: Int)

Inline classes have some, more or less apparent, restrictions though: It’s required to specify precisely one property in the primary constructor, as shown with

value

. You can’t wrap multiple values in one

inline

class. It is also prohibited to have

init

blocks in inline classes, and you cannot have properties with backing fields. Inline classes can have simple computable properties, however, which we will see later in this article.

At runtime, the wrapped type of an inline class will be used without its wrapper whenever possible. This is similar to Java’s boxed types like

Integer

or

Boolean

, which will be represented as their corresponding primitive type whenever the compiler can do that. That exactly is the great selling point for inline classes in Kotlin: When you inline a class, the class itself won’t be used in the byte code unless it’s absolutely necessary. Inlining classes drastically reduces space overhead at runtime.

Runtime representation

At runtime, an inline class can be represented as both, the wrapper type and the underlying type. As mentioned in the previous paragraph, the compiler prefers using the underlying (wrapped) type of an

inline

class to optimize the code as much as possible. This is similar to boxing between

int

and

Integer

. In certain situations, however, the compiler needs to use the wrapper itself, so it will be generated during compilation:


public final class WrappedInt {
   private final int value;

   public final int getValue() { return this.value; }

   // $FF: synthetic method
   private WrappedInt(int value) { this.value = value; }

   public static int constructor_impl(int value) { return value; }

   // $FF: synthetic method
   @NotNull
   public static final WrappedInt box_impl(int v) { return new WrappedInt(v); }

   // $FF: synthetic method
   public final int unbox_impl() { return this.value; }

   //more Object related implementations
}

This snippet shows the simplified byte code represented as Java code to show how an

inline

class looks like. Along with some obvious stuff like the

value

field and its getter, the constructor is private, and new objects will rather be created through

constructor_impl

which does not actually use the wrapper type but only returns the passed in underlying type. Finally, you can see

box_impl

and

unbox_impl

functions which, as you might expect, are used for boxing purposes. Now let’s see how this inline class wrapper gets utilized when we use the

inline

class in our code.

Using inline classes


fun take(w: WrappedInt) {
    println(w.value)
}

fun main() {
    val inlined = WrappedInt(5)
    take(inlined)
}

In this snippet, a

WrappedInt

is being created and passed to a function which prints its wrapped value. The corresponding byte code, again as Java code, looks as follows:


public static final void take_hqTGqkw(int w) {
    System.out.println(w);
}

public static final void main() {
    int inlined = WrappedInt.constructor_impl(5);
    take_hqTGqkw(inlined);
}

In the compiled code, no instance of

WrappedInt

is created. Although the static

constructor_impl

is used, it just returns an

int

which is then passed to the

take

function that also does not know anything about the type of the

inline

class which we originally had in our source code.
Note that the names of functions accepting inline class parameters are extended with a generated hash code in the byte code. This way, they can stay distinguishable from overloaded functions accepting the underlying type as a parameter:


fun take(w: WrappedInt) = println(w.value)
fun take(v: Int) = println(v.value)

To make both

take

methods available in the JVM byte code and avoid signature clashes, the compiler renames the first one to something like

take-hqTGqkw

. Note that the example above does show a ”

_

” rather than a ”

-

” since Java does not allow method names to contain the dash which is also the reason why methods accepting inline classes are not callable from Java.

Boxing of inline classes

We saw earlier that

box_impl

and

unbox_impl

functions are created for inline classes, so when do we need them? The Kotlin docs cite a rule of thumb which says:

Inline classes are boxed whenever they are used as another type.

Boxing happens, for instance, when you use your inline class as a generic type or a nullable type:


inline class WrappedInt(val value: Int)

fun take(w: WrappedInt?) {
    if (w != null) println(w.value)
}

fun main() {
    take(WrappedInt(5))
}

In this code we modified the

take

function to take a nullable

WrappedInt

and print the underlying type if the argument is not null.


public static final void take_G1XIRLQ(@Nullable WrappedInt w) {
    if (Intrinsics.areEqual(w, (Object)null) ^ true) {
        int var1 = w.unbox_impl();
        System.out.println(var1);
    }
}

public static final void main() {
    take_G1XIRLQ(WrappedInt.box_impl(WrappedInt.constructor_impl(5)));
}

In the byte code,

take

now does not accept the underlying type directly anymore. It has to work with the wrapper type instead. When printing its content,

unbox_impl

is invoked. On the caller site, we can see that

box_impl

is used to create a boxed instance of

WrappedInt

.

It should be evident that we want to avoid boxing whenever possible. Keep in mind that specific usages of inline classes and also primitive types, in general, rely on this technique and might have to be reconsidered.

Use Cases inline classes

We saw that inline classes have a huge advantage: In the best case, they reduce runtime overhead drastically since additional heap allocations are avoided. But when do we want to use wrapper types anyway?

Better typing with inline classes

Imagine an authentication method in an API that looks as follows:


fun auth(userName: String, password: String) { println("authenticating $userName.") }

In a good world, everybody would call it with a user name and their password. However, it is not far-fetched that certain users will invoke this method differently:


auth("12345", "user1")

Since both parameters are of type

String

, you may mess up their order which gets even more likely with an increasing number of arguments of course. Wrappers around these types can help you mitigate that risk, and therefore inline classes are an awesome tool:


inline class Password(val value: String)
inline class UserName(val value: String)

fun auth(userName: UserName, password: Password) { println("authenticating $userName.")}

fun main() {
    auth(UserName("user1"), Password("12345"))
    //does not compile due to type mismatch
    auth(Password("12345"), UserName("user1"))
}

The parameter list has become less confusing and, on the caller site, the compiler will not allow a mismatch. The previously described is probably the most common scenario for using inline classes. They give you simple, type-safe wrappers without introducing additional heap allocations. For these situations, inline classes should be preferred whenever possible. Nevertheless, inline classes can be even smarter, which the next use case demonstrates.

Handling state without additional space

Let’s consider a method that takes a numeric string and parses it into a

BigDecimal

while also adjusting its scale:


/**
 * parses string number into BigDecimal with a scale of 2
 */
fun parseNumber(number: String): BigDecimal {
    return number.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun main() {
    println(parseNumber("100.12212"))
}

The code is pretty straightforward and would work just fine, but a requirement could be that you need to keep somehow track of the original string that was used to parse the number. To solve that, you would probably create a wrapper type or maybe use the existing

Pair

class to return a pair of values from that function. Those approaches would be valid although it obviously allocates additional space, which, in particular situation, should be avoided.
Inline classes can help you with that. We already noticed that inline classes can’t have multiple properties with backing fields. However, they can have simple calculated members in the form of properties and functions. We can create an inline class for our use case that wraps the original

String

and provides a method or a property that, on demand, parses our value. For the user, this will look like a normal data wrapper around two types while it does not add any runtime overhead in the best case:


inline class ParsableNumber(val original: String) {
    val parsed: BigDecimal
        get() = original.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun getParsableNumber(number: String): ParsableNumber {
    return ParsableNumber(number)
}

fun main() {
    val parsableNumber = getParsableNumber("100.12212")
    println(parsableNumber.parsed)
    println(parsableNumber.original)
}

As you can see, the

getParsableNumber

method returns an instance of our

inline

class which provides two properties

original

(the underlying type) and

parsed

(the calculated parsed number). That’s an interesting use case that is worth observing on a byte code level again:

More byte code


public final class ParsableNumber {
   @NotNull
   private final String original;

   @NotNull
   public final String getOriginal() { return this.original; }

   // $FF: synthetic method
   private ParsableNumber(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      super();
      this.original = original;
   }

   @NotNull
   public static final BigDecimal getParsed_impl(String $this) {
      BigDecimal var10000 = (new BigDecimal($this)).setScale(2, RoundingMode.HALF_UP);
      Intrinsics.checkExpressionValueIsNotNull(var10000, "original.toBigDecimal().…(2, RoundingMode.HALF_UP)");
      return var10000;
   }

   @NotNull
   public static String constructor_impl(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      return original;
   }

   // $FF: synthetic method
   @NotNull
   public static final ParsableNumber box_impl(@NotNull String v) {
      Intrinsics.checkParameterIsNotNull(v, "v");
      return new ParsableNumber(v);
   }

   // $FF: synthetic method
   @NotNull
   public final String unbox_impl() { return this.original; }

    //more Object related implementations
}

The generated wrapper class

ParsableNumber

pretty much looks like the earlier shown

WrappedInt

class. One important difference, however, is the

getParsed_impl

function, which represents our computable property

parsed

. As you can see, the function is implemented as a static function that takes a string and returns a

BigDecimal

. So how is this being utilized in the caller code?


@NotNull
public static final String getParsableNumber(@NotNull String number) {
    Intrinsics.checkParameterIsNotNull(number, "number");
    return ParsableNumber.constructor_impl(number);
}

public static final void main() {
    String parsableNumber = getParsableNumber("100.12212");
    BigDecimal var1 = ParsableNumber.getParsed_impl(parsableNumber);
    System.out.println(var1);
    System.out.println(parsableNumber);
}

As expected,

getParsableNumber

does not have any reference to our wrapper type. It simply returns the

String

without introducing any new type. In the

main

, we see that the static

getParsed_impl

is used to parse the given

String

into a

BigDecimal

. Again, no usage of

ParsableNumber

.

Reducing the scope of extension functions

A common issue with extension functions is that they may pollute your namespace if defined on general types like

String

. As an example, you may want to have an extension function that converts a JSON string into a corresponding type:


inline fun <reified T> String.asJson() = jacksonObjectMapper().readValue<T>(this)

To convert a given string into some data holder

JsonData

, you would then do:


val jsonString = """{ "x":200, "y":300 }"""
val data: JsonData = jsonString.asJson()

However, the extension function is available on strings that represent other data as well although it might not make much sense:


"whatever".asJson<JsonData> //will fail

This code will fail since the

String

does not contain valid JSON data. What can we do to make the extension shown above only available for certain strings? Yep, inline classes can help with that:

Narrow down extension scope with inline class


inline class JsonString(val value: String)
inline fun <reified T> JsonString.asJson() = jacksonObjectMapper().readValue<T>(this.value)

When we introduce a wrapper for strings that holds JSON data and change the extension to using a

JsonString

receiver accordingly, the issue described above has been solved. The extension won’t anymore appear on any arbitrary

String

but only those we consciously wrapped in a

JsonString

.

Unsigned Types

Another great use case of inline classes becomes apparent when looking at the unsigned integer types that were added to the language with version 1.3, also as an experimental feature:


public inline class UInt @PublishedApi internal constructor(@PublishedApi internal val data: Int) : Comparable<UInt>

As you can see, the

UInt

class is defined as an unsigned class that wraps a normal signed integer

data

. You can learn more about this feature in the corresponding KEEP.

Conclusion

Inline classes are a great tool that can be used to reduce heap allocations for wrapper types and helps us with solving different kinds of problems. However, be aware that certain scenarios such as using inline classes as nullable types require boxing. Since inline classes are still experimental, you have to expect your code to break in future releases due to changes in their behavior. Please keep this in mind. Nevertheless, I think it’s valid to start using them right away.

If you want to learn more about Kotlin’s beautiful features I recommend the book Kotlin in Action to you and also like to direct you to my other articles 🙂

Another great article about inline classes can be found here (by Dave Leeds).

The post Kotlin Inline Classes – How they work and when you should use them appeared first on Kotlin Expertise Blog.

Continue Reading Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin introduced inline classes with version 1.3 as an experimental feature. You should be aware that their implementation can still change in future releases, but it’s already a great time to learn about them now. Inline classes add a simple tool we can use to wrap some other type without adding runtime overhead through additional heap allocations. In this article, we want to see how inline classes in Kotlin work and when it makes sense to use them.

Enable inline classes in your project

To enable inline classes in your project, you simply need to work with Kotlin version > 1.3 which adds the

inline

keyword to the language. Since inline classes are still experimental, your IDE will display related warnings when you make use of them. The warnings can be disabled with explicit compiler flags as described here.

The first glance at inline classes

Inline classes are pretty straightforward. To make a class inlined, you just add the

inline

keyword to your class:


inline class WrappedInt(val value: Int)

Inline classes have some, more or less apparent, restrictions though: It’s required to specify precisely one property in the primary constructor, as shown with

value

. You can’t wrap multiple values in one

inline

class. It is also prohibited to have

init

blocks in inline classes, and you cannot have properties with backing fields. Inline classes can have simple computable properties, however, which we will see later in this article.

At runtime, the wrapped type of an inline class will be used without its wrapper whenever possible. This is similar to Java’s boxed types like

Integer

or

Boolean

, which will be represented as their corresponding primitive type whenever the compiler can do that. That exactly is the great selling point for inline classes in Kotlin: When you inline a class, the class itself won’t be used in the byte code unless it’s absolutely necessary. Inlining classes drastically reduces space overhead at runtime.

Runtime representation

At runtime, an inline class can be represented as both, the wrapper type and the underlying type. As mentioned in the previous paragraph, the compiler prefers using the underlying (wrapped) type of an

inline

class to optimize the code as much as possible. This is similar to boxing between

int

and

Integer

. In certain situations, however, the compiler needs to use the wrapper itself, so it will be generated during compilation:


public final class WrappedInt {
   private final int value;

   public final int getValue() { return this.value; }

   // $FF: synthetic method
   private WrappedInt(int value) { this.value = value; }

   public static int constructor_impl(int value) { return value; }

   // $FF: synthetic method
   @NotNull
   public static final WrappedInt box_impl(int v) { return new WrappedInt(v); }

   // $FF: synthetic method
   public final int unbox_impl() { return this.value; }

   //more Object related implementations
}

This snippet shows the simplified byte code represented as Java code to show how an

inline

class looks like. Along with some obvious stuff like the

value

field and its getter, the constructor is private, and new objects will rather be created through

constructor_impl

which does not actually use the wrapper type but only returns the passed in underlying type. Finally, you can see

box_impl

and

unbox_impl

functions which, as you might expect, are used for boxing purposes. Now let’s see how this inline class wrapper gets utilized when we use the

inline

class in our code.

Using inline classes


fun take(w: WrappedInt) {
    println(w.value)
}

fun main() {
    val inlined = WrappedInt(5)
    take(inlined)
}

In this snippet, a

WrappedInt

is being created and passed to a function which prints its wrapped value. The corresponding byte code, again as Java code, looks as follows:


public static final void take_hqTGqkw(int w) {
    System.out.println(w);
}

public static final void main() {
    int inlined = WrappedInt.constructor_impl(5);
    take_hqTGqkw(inlined);
}

In the compiled code, no instance of

WrappedInt

is created. Although the static

constructor_impl

is used, it just returns an

int

which is then passed to the

take

function that also does not know anything about the type of the

inline

class which we originally had in our source code.
Note that the names of functions accepting inline class parameters are extended with a generated hash code in the byte code. This way, they can stay distinguishable from overloaded functions accepting the underlying type as a parameter:


fun take(w: WrappedInt) = println(w.value)
fun take(v: Int) = println(v.value)

To make both

take

methods available in the JVM byte code and avoid signature clashes, the compiler renames the first one to something like

take-hqTGqkw

. Note that the example above does show a ”

_

” rather than a ”

-

” since Java does not allow method names to contain the dash which is also the reason why methods accepting inline classes are not callable from Java.

Boxing of inline classes

We saw earlier that

box_impl

and

unbox_impl

functions are created for inline classes, so when do we need them? The Kotlin docs cite a rule of thumb which says:

Inline classes are boxed whenever they are used as another type.

Boxing happens, for instance, when you use your inline class as a generic type or a nullable type:


inline class WrappedInt(val value: Int)

fun take(w: WrappedInt?) {
    if (w != null) println(w.value)
}

fun main() {
    take(WrappedInt(5))
}

In this code we modified the

take

function to take a nullable

WrappedInt

and print the underlying type if the argument is not null.


public static final void take_G1XIRLQ(@Nullable WrappedInt w) {
    if (Intrinsics.areEqual(w, (Object)null) ^ true) {
        int var1 = w.unbox_impl();
        System.out.println(var1);
    }
}

public static final void main() {
    take_G1XIRLQ(WrappedInt.box_impl(WrappedInt.constructor_impl(5)));
}

In the byte code,

take

now does not accept the underlying type directly anymore. It has to work with the wrapper type instead. When printing its content,

unbox_impl

is invoked. On the caller site, we can see that

box_impl

is used to create a boxed instance of

WrappedInt

.

It should be evident that we want to avoid boxing whenever possible. Keep in mind that specific usages of inline classes and also primitive types, in general, rely on this technique and might have to be reconsidered.

Use Cases inline classes

We saw that inline classes have a huge advantage: In the best case, they reduce runtime overhead drastically since additional heap allocations are avoided. But when do we want to use wrapper types anyway?

Better typing with inline classes

Imagine an authentication method in an API that looks as follows:


fun auth(userName: String, password: String) { println("authenticating $userName.") }

In a good world, everybody would call it with a user name and their password. However, it is not far-fetched that certain users will invoke this method differently:


auth("12345", "user1")

Since both parameters are of type

String

, you may mess up their order which gets even more likely with an increasing number of arguments of course. Wrappers around these types can help you mitigate that risk, and therefore inline classes are an awesome tool:


inline class Password(val value: String)
inline class UserName(val value: String)

fun auth(userName: UserName, password: Password) { println("authenticating $userName.")}

fun main() {
    auth(UserName("user1"), Password("12345"))
    //does not compile due to type mismatch
    auth(Password("12345"), UserName("user1"))
}

The parameter list has become less confusing and, on the caller site, the compiler will not allow a mismatch. The previously described is probably the most common scenario for using inline classes. They give you simple, type-safe wrappers without introducing additional heap allocations. For these situations, inline classes should be preferred whenever possible. Nevertheless, inline classes can be even smarter, which the next use case demonstrates.

Handling state without additional space

Let’s consider a method that takes a numeric string and parses it into a

BigDecimal

while also adjusting its scale:


/**
 * parses string number into BigDecimal with a scale of 2
 */
fun parseNumber(number: String): BigDecimal {
    return number.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun main() {
    println(parseNumber("100.12212"))
}

The code is pretty straightforward and would work just fine, but a requirement could be that you need to keep somehow track of the original string that was used to parse the number. To solve that, you would probably create a wrapper type or maybe use the existing

Pair

class to return a pair of values from that function. Those approaches would be valid although it obviously allocates additional space, which, in particular situation, should be avoided.
Inline classes can help you with that. We already noticed that inline classes can’t have multiple properties with backing fields. However, they can have simple calculated members in the form of properties and functions. We can create an inline class for our use case that wraps the original

String

and provides a method or a property that, on demand, parses our value. For the user, this will look like a normal data wrapper around two types while it does not add any runtime overhead in the best case:


inline class ParsableNumber(val original: String) {
    val parsed: BigDecimal
        get() = original.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun getParsableNumber(number: String): ParsableNumber {
    return ParsableNumber(number)
}

fun main() {
    val parsableNumber = getParsableNumber("100.12212")
    println(parsableNumber.parsed)
    println(parsableNumber.original)
}

As you can see, the

getParsableNumber

method returns an instance of our

inline

class which provides two properties

original

(the underlying type) and

parsed

(the calculated parsed number). That’s an interesting use case that is worth observing on a byte code level again:

More byte code


public final class ParsableNumber {
   @NotNull
   private final String original;

   @NotNull
   public final String getOriginal() { return this.original; }

   // $FF: synthetic method
   private ParsableNumber(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      super();
      this.original = original;
   }

   @NotNull
   public static final BigDecimal getParsed_impl(String $this) {
      BigDecimal var10000 = (new BigDecimal($this)).setScale(2, RoundingMode.HALF_UP);
      Intrinsics.checkExpressionValueIsNotNull(var10000, "original.toBigDecimal().…(2, RoundingMode.HALF_UP)");
      return var10000;
   }

   @NotNull
   public static String constructor_impl(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      return original;
   }

   // $FF: synthetic method
   @NotNull
   public static final ParsableNumber box_impl(@NotNull String v) {
      Intrinsics.checkParameterIsNotNull(v, "v");
      return new ParsableNumber(v);
   }

   // $FF: synthetic method
   @NotNull
   public final String unbox_impl() { return this.original; }

    //more Object related implementations
}

The generated wrapper class

ParsableNumber

pretty much looks like the earlier shown

WrappedInt

class. One important difference, however, is the

getParsed_impl

function, which represents our computable property

parsed

. As you can see, the function is implemented as a static function that takes a string and returns a

BigDecimal

. So how is this being utilized in the caller code?


@NotNull
public static final String getParsableNumber(@NotNull String number) {
    Intrinsics.checkParameterIsNotNull(number, "number");
    return ParsableNumber.constructor_impl(number);
}

public static final void main() {
    String parsableNumber = getParsableNumber("100.12212");
    BigDecimal var1 = ParsableNumber.getParsed_impl(parsableNumber);
    System.out.println(var1);
    System.out.println(parsableNumber);
}

As expected,

getParsableNumber

does not have any reference to our wrapper type. It simply returns the

String

without introducing any new type. In the

main

, we see that the static

getParsed_impl

is used to parse the given

String

into a

BigDecimal

. Again, no usage of

ParsableNumber

.

Reducing the scope of extension functions

A common issue with extension functions is that they may pollute your namespace if defined on general types like

String

. As an example, you may want to have an extension function that converts a JSON string into a corresponding type:


inline fun <reified T> String.asJson() = jacksonObjectMapper().readValue<T>(this)

To convert a given string into some data holder

JsonData

, you would then do:


val jsonString = """{ "x":200, "y":300 }"""
val data: JsonData = jsonString.asJson()

However, the extension function is available on strings that represent other data as well although it might not make much sense:


"whatever".asJson<JsonData> //will fail

This code will fail since the

String

does not contain valid JSON data. What can we do to make the extension shown above only available for certain strings? Yep, inline classes can help with that:

Narrow down extension scope with inline class


inline class JsonString(val value: String)
inline fun <reified T> JsonString.asJson() = jacksonObjectMapper().readValue<T>(this.value)

When we introduce a wrapper for strings that holds JSON data and change the extension to using a

JsonString

receiver accordingly, the issue described above has been solved. The extension won’t anymore appear on any arbitrary

String

but only those we consciously wrapped in a

JsonString

.

Unsigned Types

Another great use case of inline classes becomes apparent when looking at the unsigned integer types that were added to the language with version 1.3, also as an experimental feature:


public inline class UInt @PublishedApi internal constructor(@PublishedApi internal val data: Int) : Comparable<UInt>

As you can see, the

UInt

class is defined as an unsigned class that wraps a normal signed integer

data

. You can learn more about this feature in the corresponding KEEP.

Conclusion

Inline classes are a great tool that can be used to reduce heap allocations for wrapper types and helps us with solving different kinds of problems. However, be aware that certain scenarios such as using inline classes as nullable types require boxing. Since inline classes are still experimental, you have to expect your code to break in future releases due to changes in their behavior. Please keep this in mind. Nevertheless, I think it’s valid to start using them right away.

If you want to learn more about Kotlin’s beautiful features I recommend the book Kotlin in Action to you and also like to direct you to my other articles 🙂

Another great article about inline classes can be found here (by Dave Leeds).

The post Kotlin Inline Classes – How they work and when you should use them appeared first on Kotlin Expertise Blog.

Continue Reading Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin introduced inline classes with version 1.3 as an experimental feature. You should be aware that their implementation can still change in future releases, but it’s already a great time to learn about them now. Inline classes add a simple tool we can use to wrap some other type without adding runtime overhead through additional heap allocations. In this article, we want to see how inline classes in Kotlin work and when it makes sense to use them.

Enable inline classes in your project

To enable inline classes in your project, you simply need to work with Kotlin version > 1.3 which adds the

inline

keyword to the language. Since inline classes are still experimental, your IDE will display related warnings when you make use of them. The warnings can be disabled with explicit compiler flags as described here.

The first glance at inline classes

Inline classes are pretty straightforward. To make a class inlined, you just add the

inline

keyword to your class:


inline class WrappedInt(val value: Int)

Inline classes have some, more or less apparent, restrictions though: It’s required to specify precisely one property in the primary constructor, as shown with

value

. You can’t wrap multiple values in one

inline

class. It is also prohibited to have

init

blocks in inline classes, and you cannot have properties with backing fields. Inline classes can have simple computable properties, however, which we will see later in this article.

At runtime, the wrapped type of an inline class will be used without its wrapper whenever possible. This is similar to Java’s boxed types like

Integer

or

Boolean

, which will be represented as their corresponding primitive type whenever the compiler can do that. That exactly is the great selling point for inline classes in Kotlin: When you inline a class, the class itself won’t be used in the byte code unless it’s absolutely necessary. Inlining classes drastically reduces space overhead at runtime.

Runtime representation

At runtime, an inline class can be represented as both, the wrapper type and the underlying type. As mentioned in the previous paragraph, the compiler prefers using the underlying (wrapped) type of an

inline

class to optimize the code as much as possible. This is similar to boxing between

int

and

Integer

. In certain situations, however, the compiler needs to use the wrapper itself, so it will be generated during compilation:


public final class WrappedInt {
   private final int value;

   public final int getValue() { return this.value; }

   // $FF: synthetic method
   private WrappedInt(int value) { this.value = value; }

   public static int constructor_impl(int value) { return value; }

   // $FF: synthetic method
   @NotNull
   public static final WrappedInt box_impl(int v) { return new WrappedInt(v); }

   // $FF: synthetic method
   public final int unbox_impl() { return this.value; }

   //more Object related implementations
}

This snippet shows the simplified byte code represented as Java code to show how an

inline

class looks like. Along with some obvious stuff like the

value

field and its getter, the constructor is private, and new objects will rather be created through

constructor_impl

which does not actually use the wrapper type but only returns the passed in underlying type. Finally, you can see

box_impl

and

unbox_impl

functions which, as you might expect, are used for boxing purposes. Now let’s see how this inline class wrapper gets utilized when we use the

inline

class in our code.

Using inline classes


fun take(w: WrappedInt) {
    println(w.value)
}

fun main() {
    val inlined = WrappedInt(5)
    take(inlined)
}

In this snippet, a

WrappedInt

is being created and passed to a function which prints its wrapped value. The corresponding byte code, again as Java code, looks as follows:


public static final void take_hqTGqkw(int w) {
    System.out.println(w);
}

public static final void main() {
    int inlined = WrappedInt.constructor_impl(5);
    take_hqTGqkw(inlined);
}

In the compiled code, no instance of

WrappedInt

is created. Although the static

constructor_impl

is used, it just returns an

int

which is then passed to the

take

function that also does not know anything about the type of the

inline

class which we originally had in our source code.
Note that the names of functions accepting inline class parameters are extended with a generated hash code in the byte code. This way, they can stay distinguishable from overloaded functions accepting the underlying type as a parameter:


fun take(w: WrappedInt) = println(w.value)
fun take(v: Int) = println(v.value)

To make both

take

methods available in the JVM byte code and avoid signature clashes, the compiler renames the first one to something like

take-hqTGqkw

. Note that the example above does show a ”

_

” rather than a ”

-

” since Java does not allow method names to contain the dash which is also the reason why methods accepting inline classes are not callable from Java.

Boxing of inline classes

We saw earlier that

box_impl

and

unbox_impl

functions are created for inline classes, so when do we need them? The Kotlin docs cite a rule of thumb which says:

Inline classes are boxed whenever they are used as another type.

Boxing happens, for instance, when you use your inline class as a generic type or a nullable type:


inline class WrappedInt(val value: Int)

fun take(w: WrappedInt?) {
    if (w != null) println(w.value)
}

fun main() {
    take(WrappedInt(5))
}

In this code we modified the

take

function to take a nullable

WrappedInt

and print the underlying type if the argument is not null.


public static final void take_G1XIRLQ(@Nullable WrappedInt w) {
    if (Intrinsics.areEqual(w, (Object)null) ^ true) {
        int var1 = w.unbox_impl();
        System.out.println(var1);
    }
}

public static final void main() {
    take_G1XIRLQ(WrappedInt.box_impl(WrappedInt.constructor_impl(5)));
}

In the byte code,

take

now does not accept the underlying type directly anymore. It has to work with the wrapper type instead. When printing its content,

unbox_impl

is invoked. On the caller site, we can see that

box_impl

is used to create a boxed instance of

WrappedInt

.

It should be evident that we want to avoid boxing whenever possible. Keep in mind that specific usages of inline classes and also primitive types, in general, rely on this technique and might have to be reconsidered.

Use Cases inline classes

We saw that inline classes have a huge advantage: In the best case, they reduce runtime overhead drastically since additional heap allocations are avoided. But when do we want to use wrapper types anyway?

Better typing with inline classes

Imagine an authentication method in an API that looks as follows:


fun auth(userName: String, password: String) { println("authenticating $userName.") }

In a good world, everybody would call it with a user name and their password. However, it is not far-fetched that certain users will invoke this method differently:


auth("12345", "user1")

Since both parameters are of type

String

, you may mess up their order which gets even more likely with an increasing number of arguments of course. Wrappers around these types can help you mitigate that risk, and therefore inline classes are an awesome tool:


inline class Password(val value: String)
inline class UserName(val value: String)

fun auth(userName: UserName, password: Password) { println("authenticating $userName.")}

fun main() {
    auth(UserName("user1"), Password("12345"))
    //does not compile due to type mismatch
    auth(Password("12345"), UserName("user1"))
}

The parameter list has become less confusing and, on the caller site, the compiler will not allow a mismatch. The previously described is probably the most common scenario for using inline classes. They give you simple, type-safe wrappers without introducing additional heap allocations. For these situations, inline classes should be preferred whenever possible. Nevertheless, inline classes can be even smarter, which the next use case demonstrates.

Handling state without additional space

Let’s consider a method that takes a numeric string and parses it into a

BigDecimal

while also adjusting its scale:


/**
 * parses string number into BigDecimal with a scale of 2
 */
fun parseNumber(number: String): BigDecimal {
    return number.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun main() {
    println(parseNumber("100.12212"))
}

The code is pretty straightforward and would work just fine, but a requirement could be that you need to keep somehow track of the original string that was used to parse the number. To solve that, you would probably create a wrapper type or maybe use the existing

Pair

class to return a pair of values from that function. Those approaches would be valid although it obviously allocates additional space, which, in particular situation, should be avoided.
Inline classes can help you with that. We already noticed that inline classes can’t have multiple properties with backing fields. However, they can have simple calculated members in the form of properties and functions. We can create an inline class for our use case that wraps the original

String

and provides a method or a property that, on demand, parses our value. For the user, this will look like a normal data wrapper around two types while it does not add any runtime overhead in the best case:


inline class ParsableNumber(val original: String) {
    val parsed: BigDecimal
        get() = original.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun getParsableNumber(number: String): ParsableNumber {
    return ParsableNumber(number)
}

fun main() {
    val parsableNumber = getParsableNumber("100.12212")
    println(parsableNumber.parsed)
    println(parsableNumber.original)
}

As you can see, the

getParsableNumber

method returns an instance of our

inline

class which provides two properties

original

(the underlying type) and

parsed

(the calculated parsed number). That’s an interesting use case that is worth observing on a byte code level again:

More byte code


public final class ParsableNumber {
   @NotNull
   private final String original;

   @NotNull
   public final String getOriginal() { return this.original; }

   // $FF: synthetic method
   private ParsableNumber(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      super();
      this.original = original;
   }

   @NotNull
   public static final BigDecimal getParsed_impl(String $this) {
      BigDecimal var10000 = (new BigDecimal($this)).setScale(2, RoundingMode.HALF_UP);
      Intrinsics.checkExpressionValueIsNotNull(var10000, "original.toBigDecimal().…(2, RoundingMode.HALF_UP)");
      return var10000;
   }

   @NotNull
   public static String constructor_impl(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      return original;
   }

   // $FF: synthetic method
   @NotNull
   public static final ParsableNumber box_impl(@NotNull String v) {
      Intrinsics.checkParameterIsNotNull(v, "v");
      return new ParsableNumber(v);
   }

   // $FF: synthetic method
   @NotNull
   public final String unbox_impl() { return this.original; }

    //more Object related implementations
}

The generated wrapper class

ParsableNumber

pretty much looks like the earlier shown

WrappedInt

class. One important difference, however, is the

getParsed_impl

function, which represents our computable property

parsed

. As you can see, the function is implemented as a static function that takes a string and returns a

BigDecimal

. So how is this being utilized in the caller code?


@NotNull
public static final String getParsableNumber(@NotNull String number) {
    Intrinsics.checkParameterIsNotNull(number, "number");
    return ParsableNumber.constructor_impl(number);
}

public static final void main() {
    String parsableNumber = getParsableNumber("100.12212");
    BigDecimal var1 = ParsableNumber.getParsed_impl(parsableNumber);
    System.out.println(var1);
    System.out.println(parsableNumber);
}

As expected,

getParsableNumber

does not have any reference to our wrapper type. It simply returns the

String

without introducing any new type. In the

main

, we see that the static

getParsed_impl

is used to parse the given

String

into a

BigDecimal

. Again, no usage of

ParsableNumber

.

Reducing the scope of extension functions

A common issue with extension functions is that they may pollute your namespace if defined on general types like

String

. As an example, you may want to have an extension function that converts a JSON string into a corresponding type:


inline fun <reified T> String.asJson() = jacksonObjectMapper().readValue<T>(this)

To convert a given string into some data holder

JsonData

, you would then do:


val jsonString = """{ "x":200, "y":300 }"""
val data: JsonData = jsonString.asJson()

However, the extension function is available on strings that represent other data as well although it might not make much sense:


"whatever".asJson<JsonData> //will fail

This code will fail since the

String

does not contain valid JSON data. What can we do to make the extension shown above only available for certain strings? Yep, inline classes can help with that:

Narrow down extension scope with inline class


inline class JsonString(val value: String)
inline fun <reified T> JsonString.asJson() = jacksonObjectMapper().readValue<T>(this.value)

When we introduce a wrapper for strings that holds JSON data and change the extension to using a

JsonString

receiver accordingly, the issue described above has been solved. The extension won’t anymore appear on any arbitrary

String

but only those we consciously wrapped in a

JsonString

.

Unsigned Types

Another great use case of inline classes becomes apparent when looking at the unsigned integer types that were added to the language with version 1.3, also as an experimental feature:


public inline class UInt @PublishedApi internal constructor(@PublishedApi internal val data: Int) : Comparable<UInt>

As you can see, the

UInt

class is defined as an unsigned class that wraps a normal signed integer

data

. You can learn more about this feature in the corresponding KEEP.

Conclusion

Inline classes are a great tool that can be used to reduce heap allocations for wrapper types and helps us with solving different kinds of problems. However, be aware that certain scenarios such as using inline classes as nullable types require boxing. Since inline classes are still experimental, you have to expect your code to break in future releases due to changes in their behavior. Please keep this in mind. Nevertheless, I think it’s valid to start using them right away.

If you want to learn more about Kotlin’s beautiful features I recommend the book Kotlin in Action to you and also like to direct you to my other articles 🙂

Another great article about inline classes can be found here (by Dave Leeds).

The post Kotlin Inline Classes – How they work and when you should use them appeared first on Kotlin Expertise Blog.

Continue Reading Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin introduced inline classes with version 1.3 as an experimental feature. You should be aware that their implementation can still change in future releases, but it’s already a great time to learn about them now. Inline classes add a simple tool we can use to wrap some other type without adding runtime overhead through additional heap allocations. In this article, we want to see how inline classes in Kotlin work and when it makes sense to use them.

Enable inline classes in your project

To enable inline classes in your project, you simply need to work with Kotlin version > 1.3 which adds the

inline

keyword to the language. Since inline classes are still experimental, your IDE will display related warnings when you make use of them. The warnings can be disabled with explicit compiler flags as described here.

The first glance at inline classes

Inline classes are pretty straightforward. To make a class inlined, you just add the

inline

keyword to your class:


inline class WrappedInt(val value: Int)

Inline classes have some, more or less apparent, restrictions though: It’s required to specify precisely one property in the primary constructor, as shown with

value

. You can’t wrap multiple values in one

inline

class. It is also prohibited to have

init

blocks in inline classes, and you cannot have properties with backing fields. Inline classes can have simple computable properties, however, which we will see later in this article.

At runtime, the wrapped type of an inline class will be used without its wrapper whenever possible. This is similar to Java’s boxed types like

Integer

or

Boolean

, which will be represented as their corresponding primitive type whenever the compiler can do that. That exactly is the great selling point for inline classes in Kotlin: When you inline a class, the class itself won’t be used in the byte code unless it’s absolutely necessary. Inlining classes drastically reduces space overhead at runtime.

Runtime representation

At runtime, an inline class can be represented as both, the wrapper type and the underlying type. As mentioned in the previous paragraph, the compiler prefers using the underlying (wrapped) type of an

inline

class to optimize the code as much as possible. This is similar to boxing between

int

and

Integer

. In certain situations, however, the compiler needs to use the wrapper itself, so it will be generated during compilation:


public final class WrappedInt {
   private final int value;

   public final int getValue() { return this.value; }

   // $FF: synthetic method
   private WrappedInt(int value) { this.value = value; }

   public static int constructor_impl(int value) { return value; }

   // $FF: synthetic method
   @NotNull
   public static final WrappedInt box_impl(int v) { return new WrappedInt(v); }

   // $FF: synthetic method
   public final int unbox_impl() { return this.value; }

   //more Object related implementations
}

This snippet shows the simplified byte code represented as Java code to show how an

inline

class looks like. Along with some obvious stuff like the

value

field and its getter, the constructor is private, and new objects will rather be created through

constructor_impl

which does not actually use the wrapper type but only returns the passed in underlying type. Finally, you can see

box_impl

and

unbox_impl

functions which, as you might expect, are used for boxing purposes. Now let’s see how this inline class wrapper gets utilized when we use the

inline

class in our code.

Using inline classes


fun take(w: WrappedInt) {
    println(w.value)
}

fun main() {
    val inlined = WrappedInt(5)
    take(inlined)
}

In this snippet, a

WrappedInt

is being created and passed to a function which prints its wrapped value. The corresponding byte code, again as Java code, looks as follows:


public static final void take_hqTGqkw(int w) {
    System.out.println(w);
}

public static final void main() {
    int inlined = WrappedInt.constructor_impl(5);
    take_hqTGqkw(inlined);
}

In the compiled code, no instance of

WrappedInt

is created. Although the static

constructor_impl

is used, it just returns an

int

which is then passed to the

take

function that also does not know anything about the type of the

inline

class which we originally had in our source code.
Note that the names of functions accepting inline class parameters are extended with a generated hash code in the byte code. This way, they can stay distinguishable from overloaded functions accepting the underlying type as a parameter:


fun take(w: WrappedInt) = println(w.value)
fun take(v: Int) = println(v.value)

To make both

take

methods available in the JVM byte code and avoid signature clashes, the compiler renames the first one to something like

take-hqTGqkw

. Note that the example above does show a ”

_

” rather than a ”

-

” since Java does not allow method names to contain the dash which is also the reason why methods accepting inline classes are not callable from Java.

Boxing of inline classes

We saw earlier that

box_impl

and

unbox_impl

functions are created for inline classes, so when do we need them? The Kotlin docs cite a rule of thumb which says:

Inline classes are boxed whenever they are used as another type.

Boxing happens, for instance, when you use your inline class as a generic type or a nullable type:


inline class WrappedInt(val value: Int)

fun take(w: WrappedInt?) {
    if (w != null) println(w.value)
}

fun main() {
    take(WrappedInt(5))
}

In this code we modified the

take

function to take a nullable

WrappedInt

and print the underlying type if the argument is not null.


public static final void take_G1XIRLQ(@Nullable WrappedInt w) {
    if (Intrinsics.areEqual(w, (Object)null) ^ true) {
        int var1 = w.unbox_impl();
        System.out.println(var1);
    }
}

public static final void main() {
    take_G1XIRLQ(WrappedInt.box_impl(WrappedInt.constructor_impl(5)));
}

In the byte code,

take

now does not accept the underlying type directly anymore. It has to work with the wrapper type instead. When printing its content,

unbox_impl

is invoked. On the caller site, we can see that

box_impl

is used to create a boxed instance of

WrappedInt

.

It should be evident that we want to avoid boxing whenever possible. Keep in mind that specific usages of inline classes and also primitive types, in general, rely on this technique and might have to be reconsidered.

Use Cases inline classes

We saw that inline classes have a huge advantage: In the best case, they reduce runtime overhead drastically since additional heap allocations are avoided. But when do we want to use wrapper types anyway?

Better typing with inline classes

Imagine an authentication method in an API that looks as follows:


fun auth(userName: String, password: String) { println("authenticating $userName.") }

In a good world, everybody would call it with a user name and their password. However, it is not far-fetched that certain users will invoke this method differently:


auth("12345", "user1")

Since both parameters are of type

String

, you may mess up their order which gets even more likely with an increasing number of arguments of course. Wrappers around these types can help you mitigate that risk, and therefore inline classes are an awesome tool:


inline class Password(val value: String)
inline class UserName(val value: String)

fun auth(userName: UserName, password: Password) { println("authenticating $userName.")}

fun main() {
    auth(UserName("user1"), Password("12345"))
    //does not compile due to type mismatch
    auth(Password("12345"), UserName("user1"))
}

The parameter list has become less confusing and, on the caller site, the compiler will not allow a mismatch. The previously described is probably the most common scenario for using inline classes. They give you simple, type-safe wrappers without introducing additional heap allocations. For these situations, inline classes should be preferred whenever possible. Nevertheless, inline classes can be even smarter, which the next use case demonstrates.

Handling state without additional space

Let’s consider a method that takes a numeric string and parses it into a

BigDecimal

while also adjusting its scale:


/**
 * parses string number into BigDecimal with a scale of 2
 */
fun parseNumber(number: String): BigDecimal {
    return number.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun main() {
    println(parseNumber("100.12212"))
}

The code is pretty straightforward and would work just fine, but a requirement could be that you need to keep somehow track of the original string that was used to parse the number. To solve that, you would probably create a wrapper type or maybe use the existing

Pair

class to return a pair of values from that function. Those approaches would be valid although it obviously allocates additional space, which, in particular situation, should be avoided.
Inline classes can help you with that. We already noticed that inline classes can’t have multiple properties with backing fields. However, they can have simple calculated members in the form of properties and functions. We can create an inline class for our use case that wraps the original

String

and provides a method or a property that, on demand, parses our value. For the user, this will look like a normal data wrapper around two types while it does not add any runtime overhead in the best case:


inline class ParsableNumber(val original: String) {
    val parsed: BigDecimal
        get() = original.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun getParsableNumber(number: String): ParsableNumber {
    return ParsableNumber(number)
}

fun main() {
    val parsableNumber = getParsableNumber("100.12212")
    println(parsableNumber.parsed)
    println(parsableNumber.original)
}

As you can see, the

getParsableNumber

method returns an instance of our

inline

class which provides two properties

original

(the underlying type) and

parsed

(the calculated parsed number). That’s an interesting use case that is worth observing on a byte code level again:

More byte code


public final class ParsableNumber {
   @NotNull
   private final String original;

   @NotNull
   public final String getOriginal() { return this.original; }

   // $FF: synthetic method
   private ParsableNumber(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      super();
      this.original = original;
   }

   @NotNull
   public static final BigDecimal getParsed_impl(String $this) {
      BigDecimal var10000 = (new BigDecimal($this)).setScale(2, RoundingMode.HALF_UP);
      Intrinsics.checkExpressionValueIsNotNull(var10000, "original.toBigDecimal().…(2, RoundingMode.HALF_UP)");
      return var10000;
   }

   @NotNull
   public static String constructor_impl(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      return original;
   }

   // $FF: synthetic method
   @NotNull
   public static final ParsableNumber box_impl(@NotNull String v) {
      Intrinsics.checkParameterIsNotNull(v, "v");
      return new ParsableNumber(v);
   }

   // $FF: synthetic method
   @NotNull
   public final String unbox_impl() { return this.original; }

    //more Object related implementations
}

The generated wrapper class

ParsableNumber

pretty much looks like the earlier shown

WrappedInt

class. One important difference, however, is the

getParsed_impl

function, which represents our computable property

parsed

. As you can see, the function is implemented as a static function that takes a string and returns a

BigDecimal

. So how is this being utilized in the caller code?


@NotNull
public static final String getParsableNumber(@NotNull String number) {
    Intrinsics.checkParameterIsNotNull(number, "number");
    return ParsableNumber.constructor_impl(number);
}

public static final void main() {
    String parsableNumber = getParsableNumber("100.12212");
    BigDecimal var1 = ParsableNumber.getParsed_impl(parsableNumber);
    System.out.println(var1);
    System.out.println(parsableNumber);
}

As expected,

getParsableNumber

does not have any reference to our wrapper type. It simply returns the

String

without introducing any new type. In the

main

, we see that the static

getParsed_impl

is used to parse the given

String

into a

BigDecimal

. Again, no usage of

ParsableNumber

.

Reducing the scope of extension functions

A common issue with extension functions is that they may pollute your namespace if defined on general types like

String

. As an example, you may want to have an extension function that converts a JSON string into a corresponding type:


inline fun <reified T> String.asJson() = jacksonObjectMapper().readValue<T>(this)

To convert a given string into some data holder

JsonData

, you would then do:


val jsonString = """{ "x":200, "y":300 }"""
val data: JsonData = jsonString.asJson()

However, the extension function is available on strings that represent other data as well although it might not make much sense:


"whatever".asJson<JsonData> //will fail

This code will fail since the

String

does not contain valid JSON data. What can we do to make the extension shown above only available for certain strings? Yep, inline classes can help with that:

Narrow down extension scope with inline class


inline class JsonString(val value: String)
inline fun <reified T> JsonString.asJson() = jacksonObjectMapper().readValue<T>(this.value)

When we introduce a wrapper for strings that holds JSON data and change the extension to using a

JsonString

receiver accordingly, the issue described above has been solved. The extension won’t anymore appear on any arbitrary

String

but only those we consciously wrapped in a

JsonString

.

Unsigned Types

Another great use case of inline classes becomes apparent when looking at the unsigned integer types that were added to the language with version 1.3, also as an experimental feature:


public inline class UInt @PublishedApi internal constructor(@PublishedApi internal val data: Int) : Comparable<UInt>

As you can see, the

UInt

class is defined as an unsigned class that wraps a normal signed integer

data

. You can learn more about this feature in the corresponding KEEP.

Conclusion

Inline classes are a great tool that can be used to reduce heap allocations for wrapper types and helps us with solving different kinds of problems. However, be aware that certain scenarios such as using inline classes as nullable types require boxing. Since inline classes are still experimental, you have to expect your code to break in future releases due to changes in their behavior. Please keep this in mind. Nevertheless, I think it’s valid to start using them right away.

If you want to learn more about Kotlin’s beautiful features I recommend the book Kotlin in Action to you and also like to direct you to my other articles 🙂

Another great article about inline classes can be found here (by Dave Leeds).

The post Kotlin Inline Classes – How they work and when you should use them appeared first on Kotlin Expertise Blog.

Continue Reading Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin introduced inline classes with version 1.3 as an experimental feature. You should be aware that their implementation can still change in future releases, but it’s already a great time to learn about them now. Inline classes add a simple tool we can use to wrap some other type without adding runtime overhead through additional heap allocations. In this article, we want to see how inline classes in Kotlin work and when it makes sense to use them.

Enable inline classes in your project

To enable inline classes in your project, you simply need to work with Kotlin version > 1.3 which adds the

inline

keyword to the language. Since inline classes are still experimental, your IDE will display related warnings when you make use of them. The warnings can be disabled with explicit compiler flags as described here.

The first glance at inline classes

Inline classes are pretty straightforward. To make a class inlined, you just add the

inline

keyword to your class:


inline class WrappedInt(val value: Int)

Inline classes have some, more or less apparent, restrictions though: It’s required to specify precisely one property in the primary constructor, as shown with

value

. You can’t wrap multiple values in one

inline

class. It is also prohibited to have

init

blocks in inline classes, and you cannot have properties with backing fields. Inline classes can have simple computable properties, however, which we will see later in this article.

At runtime, the wrapped type of an inline class will be used without its wrapper whenever possible. This is similar to Java’s boxed types like

Integer

or

Boolean

, which will be represented as their corresponding primitive type whenever the compiler can do that. That exactly is the great selling point for inline classes in Kotlin: When you inline a class, the class itself won’t be used in the byte code unless it’s absolutely necessary. Inlining classes drastically reduces space overhead at runtime.

Runtime representation

At runtime, an inline class can be represented as both, the wrapper type and the underlying type. As mentioned in the previous paragraph, the compiler prefers using the underlying (wrapped) type of an

inline

class to optimize the code as much as possible. This is similar to boxing between

int

and

Integer

. In certain situations, however, the compiler needs to use the wrapper itself, so it will be generated during compilation:


public final class WrappedInt {
   private final int value;

   public final int getValue() { return this.value; }

   // $FF: synthetic method
   private WrappedInt(int value) { this.value = value; }

   public static int constructor_impl(int value) { return value; }

   // $FF: synthetic method
   @NotNull
   public static final WrappedInt box_impl(int v) { return new WrappedInt(v); }

   // $FF: synthetic method
   public final int unbox_impl() { return this.value; }

   //more Object related implementations
}

This snippet shows the simplified byte code represented as Java code to show how an

inline

class looks like. Along with some obvious stuff like the

value

field and its getter, the constructor is private, and new objects will rather be created through

constructor_impl

which does not actually use the wrapper type but only returns the passed in underlying type. Finally, you can see

box_impl

and

unbox_impl

functions which, as you might expect, are used for boxing purposes. Now let’s see how this inline class wrapper gets utilized when we use the

inline

class in our code.

Using inline classes


fun take(w: WrappedInt) {
    println(w.value)
}

fun main() {
    val inlined = WrappedInt(5)
    take(inlined)
}

In this snippet, a

WrappedInt

is being created and passed to a function which prints its wrapped value. The corresponding byte code, again as Java code, looks as follows:


public static final void take_hqTGqkw(int w) {
    System.out.println(w);
}

public static final void main() {
    int inlined = WrappedInt.constructor_impl(5);
    take_hqTGqkw(inlined);
}

In the compiled code, no instance of

WrappedInt

is created. Although the static

constructor_impl

is used, it just returns an

int

which is then passed to the

take

function that also does not know anything about the type of the

inline

class which we originally had in our source code.
Note that the names of functions accepting inline class parameters are extended with a generated hash code in the byte code. This way, they can stay distinguishable from overloaded functions accepting the underlying type as a parameter:


fun take(w: WrappedInt) = println(w.value)
fun take(v: Int) = println(v.value)

To make both

take

methods available in the JVM byte code and avoid signature clashes, the compiler renames the first one to something like

take-hqTGqkw

. Note that the example above does show a ”

_

” rather than a ”

-

” since Java does not allow method names to contain the dash which is also the reason why methods accepting inline classes are not callable from Java.

Boxing of inline classes

We saw earlier that

box_impl

and

unbox_impl

functions are created for inline classes, so when do we need them? The Kotlin docs cite a rule of thumb which says:

Inline classes are boxed whenever they are used as another type.

Boxing happens, for instance, when you use your inline class as a generic type or a nullable type:


inline class WrappedInt(val value: Int)

fun take(w: WrappedInt?) {
    if (w != null) println(w.value)
}

fun main() {
    take(WrappedInt(5))
}

In this code we modified the

take

function to take a nullable

WrappedInt

and print the underlying type if the argument is not null.


public static final void take_G1XIRLQ(@Nullable WrappedInt w) {
    if (Intrinsics.areEqual(w, (Object)null) ^ true) {
        int var1 = w.unbox_impl();
        System.out.println(var1);
    }
}

public static final void main() {
    take_G1XIRLQ(WrappedInt.box_impl(WrappedInt.constructor_impl(5)));
}

In the byte code,

take

now does not accept the underlying type directly anymore. It has to work with the wrapper type instead. When printing its content,

unbox_impl

is invoked. On the caller site, we can see that

box_impl

is used to create a boxed instance of

WrappedInt

.

It should be evident that we want to avoid boxing whenever possible. Keep in mind that specific usages of inline classes and also primitive types, in general, rely on this technique and might have to be reconsidered.

Use Cases inline classes

We saw that inline classes have a huge advantage: In the best case, they reduce runtime overhead drastically since additional heap allocations are avoided. But when do we want to use wrapper types anyway?

Better typing with inline classes

Imagine an authentication method in an API that looks as follows:


fun auth(userName: String, password: String) { println("authenticating $userName.") }

In a good world, everybody would call it with a user name and their password. However, it is not far-fetched that certain users will invoke this method differently:


auth("12345", "user1")

Since both parameters are of type

String

, you may mess up their order which gets even more likely with an increasing number of arguments of course. Wrappers around these types can help you mitigate that risk, and therefore inline classes are an awesome tool:


inline class Password(val value: String)
inline class UserName(val value: String)

fun auth(userName: UserName, password: Password) { println("authenticating $userName.")}

fun main() {
    auth(UserName("user1"), Password("12345"))
    //does not compile due to type mismatch
    auth(Password("12345"), UserName("user1"))
}

The parameter list has become less confusing and, on the caller site, the compiler will not allow a mismatch. The previously described is probably the most common scenario for using inline classes. They give you simple, type-safe wrappers without introducing additional heap allocations. For these situations, inline classes should be preferred whenever possible. Nevertheless, inline classes can be even smarter, which the next use case demonstrates.

Handling state without additional space

Let’s consider a method that takes a numeric string and parses it into a

BigDecimal

while also adjusting its scale:


/**
 * parses string number into BigDecimal with a scale of 2
 */
fun parseNumber(number: String): BigDecimal {
    return number.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun main() {
    println(parseNumber("100.12212"))
}

The code is pretty straightforward and would work just fine, but a requirement could be that you need to keep somehow track of the original string that was used to parse the number. To solve that, you would probably create a wrapper type or maybe use the existing

Pair

class to return a pair of values from that function. Those approaches would be valid although it obviously allocates additional space, which, in particular situation, should be avoided.
Inline classes can help you with that. We already noticed that inline classes can’t have multiple properties with backing fields. However, they can have simple calculated members in the form of properties and functions. We can create an inline class for our use case that wraps the original

String

and provides a method or a property that, on demand, parses our value. For the user, this will look like a normal data wrapper around two types while it does not add any runtime overhead in the best case:


inline class ParsableNumber(val original: String) {
    val parsed: BigDecimal
        get() = original.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun getParsableNumber(number: String): ParsableNumber {
    return ParsableNumber(number)
}

fun main() {
    val parsableNumber = getParsableNumber("100.12212")
    println(parsableNumber.parsed)
    println(parsableNumber.original)
}

As you can see, the

getParsableNumber

method returns an instance of our

inline

class which provides two properties

original

(the underlying type) and

parsed

(the calculated parsed number). That’s an interesting use case that is worth observing on a byte code level again:

More byte code


public final class ParsableNumber {
   @NotNull
   private final String original;

   @NotNull
   public final String getOriginal() { return this.original; }

   // $FF: synthetic method
   private ParsableNumber(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      super();
      this.original = original;
   }

   @NotNull
   public static final BigDecimal getParsed_impl(String $this) {
      BigDecimal var10000 = (new BigDecimal($this)).setScale(2, RoundingMode.HALF_UP);
      Intrinsics.checkExpressionValueIsNotNull(var10000, "original.toBigDecimal().…(2, RoundingMode.HALF_UP)");
      return var10000;
   }

   @NotNull
   public static String constructor_impl(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      return original;
   }

   // $FF: synthetic method
   @NotNull
   public static final ParsableNumber box_impl(@NotNull String v) {
      Intrinsics.checkParameterIsNotNull(v, "v");
      return new ParsableNumber(v);
   }

   // $FF: synthetic method
   @NotNull
   public final String unbox_impl() { return this.original; }

    //more Object related implementations
}

The generated wrapper class

ParsableNumber

pretty much looks like the earlier shown

WrappedInt

class. One important difference, however, is the

getParsed_impl

function, which represents our computable property

parsed

. As you can see, the function is implemented as a static function that takes a string and returns a

BigDecimal

. So how is this being utilized in the caller code?


@NotNull
public static final String getParsableNumber(@NotNull String number) {
    Intrinsics.checkParameterIsNotNull(number, "number");
    return ParsableNumber.constructor_impl(number);
}

public static final void main() {
    String parsableNumber = getParsableNumber("100.12212");
    BigDecimal var1 = ParsableNumber.getParsed_impl(parsableNumber);
    System.out.println(var1);
    System.out.println(parsableNumber);
}

As expected,

getParsableNumber

does not have any reference to our wrapper type. It simply returns the

String

without introducing any new type. In the

main

, we see that the static

getParsed_impl

is used to parse the given

String

into a

BigDecimal

. Again, no usage of

ParsableNumber

.

Reducing the scope of extension functions

A common issue with extension functions is that they may pollute your namespace if defined on general types like

String

. As an example, you may want to have an extension function that converts a JSON string into a corresponding type:


inline fun <reified T> String.asJson() = jacksonObjectMapper().readValue<T>(this)

To convert a given string into some data holder

JsonData

, you would then do:


val jsonString = """{ "x":200, "y":300 }"""
val data: JsonData = jsonString.asJson()

However, the extension function is available on strings that represent other data as well although it might not make much sense:


"whatever".asJson<JsonData> //will fail

This code will fail since the

String

does not contain valid JSON data. What can we do to make the extension shown above only available for certain strings? Yep, inline classes can help with that:

Narrow down extension scope with inline class


inline class JsonString(val value: String)
inline fun <reified T> JsonString.asJson() = jacksonObjectMapper().readValue<T>(this.value)

When we introduce a wrapper for strings that holds JSON data and change the extension to using a

JsonString

receiver accordingly, the issue described above has been solved. The extension won’t anymore appear on any arbitrary

String

but only those we consciously wrapped in a

JsonString

.

Unsigned Types

Another great use case of inline classes becomes apparent when looking at the unsigned integer types that were added to the language with version 1.3, also as an experimental feature:


public inline class UInt @PublishedApi internal constructor(@PublishedApi internal val data: Int) : Comparable<UInt>

As you can see, the

UInt

class is defined as an unsigned class that wraps a normal signed integer

data

. You can learn more about this feature in the corresponding KEEP.

Conclusion

Inline classes are a great tool that can be used to reduce heap allocations for wrapper types and helps us with solving different kinds of problems. However, be aware that certain scenarios such as using inline classes as nullable types require boxing. Since inline classes are still experimental, you have to expect your code to break in future releases due to changes in their behavior. Please keep this in mind. Nevertheless, I think it’s valid to start using them right away.

If you want to learn more about Kotlin’s beautiful features I recommend the book Kotlin in Action to you and also like to direct you to my other articles 🙂

Another great article about inline classes can be found here (by Dave Leeds).

The post Kotlin Inline Classes – How they work and when you should use them appeared first on Kotlin Expertise Blog.

Continue Reading Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin Inline Classes – How they work and when you should use them

Kotlin introduced inline classes with version 1.3 as an experimental feature. You should be aware that their implementation can still change in future releases, but it’s already a great time to learn about them now. Inline classes add a simple tool we can use to wrap some other type without adding runtime overhead through additional heap allocations. In this article, we want to see how inline classes in Kotlin work and when it makes sense to use them.

Enable inline classes in your project

To enable inline classes in your project, you simply need to work with Kotlin version > 1.3 which adds the

inline

keyword to the language. Since inline classes are still experimental, your IDE will display related warnings when you make use of them. The warnings can be disabled with explicit compiler flags as described here.

The first glance at inline classes

Inline classes are pretty straightforward. To make a class inlined, you just add the

inline

keyword to your class:


inline class WrappedInt(val value: Int)

Inline classes have some, more or less apparent, restrictions though: It’s required to specify precisely one property in the primary constructor, as shown with

value

. You can’t wrap multiple values in one

inline

class. It is also prohibited to have

init

blocks in inline classes, and you cannot have properties with backing fields. Inline classes can have simple computable properties, however, which we will see later in this article.

At runtime, the wrapped type of an inline class will be used without its wrapper whenever possible. This is similar to Java’s boxed types like

Integer

or

Boolean

, which will be represented as their corresponding primitive type whenever the compiler can do that. That exactly is the great selling point for inline classes in Kotlin: When you inline a class, the class itself won’t be used in the byte code unless it’s absolutely necessary. Inlining classes drastically reduces space overhead at runtime.

Runtime representation

At runtime, an inline class can be represented as both, the wrapper type and the underlying type. As mentioned in the previous paragraph, the compiler prefers using the underlying (wrapped) type of an

inline

class to optimize the code as much as possible. This is similar to boxing between

int

and

Integer

. In certain situations, however, the compiler needs to use the wrapper itself, so it will be generated during compilation:


public final class WrappedInt {
   private final int value;

   public final int getValue() { return this.value; }

   // $FF: synthetic method
   private WrappedInt(int value) { this.value = value; }

   public static int constructor_impl(int value) { return value; }

   // $FF: synthetic method
   @NotNull
   public static final WrappedInt box_impl(int v) { return new WrappedInt(v); }

   // $FF: synthetic method
   public final int unbox_impl() { return this.value; }

   //more Object related implementations
}

This snippet shows the simplified byte code represented as Java code to show how an

inline

class looks like. Along with some obvious stuff like the

value

field and its getter, the constructor is private, and new objects will rather be created through

constructor_impl

which does not actually use the wrapper type but only returns the passed in underlying type. Finally, you can see

box_impl

and

unbox_impl

functions which, as you might expect, are used for boxing purposes. Now let’s see how this inline class wrapper gets utilized when we use the

inline

class in our code.

Using inline classes


fun take(w: WrappedInt) {
    println(w.value)
}

fun main() {
    val inlined = WrappedInt(5)
    take(inlined)
}

In this snippet, a

WrappedInt

is being created and passed to a function which prints its wrapped value. The corresponding byte code, again as Java code, looks as follows:


public static final void take_hqTGqkw(int w) {
    System.out.println(w);
}

public static final void main() {
    int inlined = WrappedInt.constructor_impl(5);
    take_hqTGqkw(inlined);
}

In the compiled code, no instance of

WrappedInt

is created. Although the static

constructor_impl

is used, it just returns an

int

which is then passed to the

take

function that also does not know anything about the type of the

inline

class which we originally had in our source code.
Note that the names of functions accepting inline class parameters are extended with a generated hash code in the byte code. This way, they can stay distinguishable from overloaded functions accepting the underlying type as a parameter:


fun take(w: WrappedInt) = println(w.value)
fun take(v: Int) = println(v.value)

To make both

take

methods available in the JVM byte code and avoid signature clashes, the compiler renames the first one to something like

take-hqTGqkw

. Note that the example above does show a ”

_

” rather than a ”

-

” since Java does not allow method names to contain the dash which is also the reason why methods accepting inline classes are not callable from Java.

Boxing of inline classes

We saw earlier that

box_impl

and

unbox_impl

functions are created for inline classes, so when do we need them? The Kotlin docs cite a rule of thumb which says:

Inline classes are boxed whenever they are used as another type.

Boxing happens, for instance, when you use your inline class as a generic type or a nullable type:


inline class WrappedInt(val value: Int)

fun take(w: WrappedInt?) {
    if (w != null) println(w.value)
}

fun main() {
    take(WrappedInt(5))
}

In this code we modified the

take

function to take a nullable

WrappedInt

and print the underlying type if the argument is not null.


public static final void take_G1XIRLQ(@Nullable WrappedInt w) {
    if (Intrinsics.areEqual(w, (Object)null) ^ true) {
        int var1 = w.unbox_impl();
        System.out.println(var1);
    }
}

public static final void main() {
    take_G1XIRLQ(WrappedInt.box_impl(WrappedInt.constructor_impl(5)));
}

In the byte code,

take

now does not accept the underlying type directly anymore. It has to work with the wrapper type instead. When printing its content,

unbox_impl

is invoked. On the caller site, we can see that

box_impl

is used to create a boxed instance of

WrappedInt

.

It should be evident that we want to avoid boxing whenever possible. Keep in mind that specific usages of inline classes and also primitive types, in general, rely on this technique and might have to be reconsidered.

Use Cases inline classes

We saw that inline classes have a huge advantage: In the best case, they reduce runtime overhead drastically since additional heap allocations are avoided. But when do we want to use wrapper types anyway?

Better typing with inline classes

Imagine an authentication method in an API that looks as follows:


fun auth(userName: String, password: String) { println("authenticating $userName.") }

In a good world, everybody would call it with a user name and their password. However, it is not far-fetched that certain users will invoke this method differently:


auth("12345", "user1")

Since both parameters are of type

String

, you may mess up their order which gets even more likely with an increasing number of arguments of course. Wrappers around these types can help you mitigate that risk, and therefore inline classes are an awesome tool:


inline class Password(val value: String)
inline class UserName(val value: String)

fun auth(userName: UserName, password: Password) { println("authenticating $userName.")}

fun main() {
    auth(UserName("user1"), Password("12345"))
    //does not compile due to type mismatch
    auth(Password("12345"), UserName("user1"))
}

The parameter list has become less confusing and, on the caller site, the compiler will not allow a mismatch. The previously described is probably the most common scenario for using inline classes. They give you simple, type-safe wrappers without introducing additional heap allocations. For these situations, inline classes should be preferred whenever possible. Nevertheless, inline classes can be even smarter, which the next use case demonstrates.

Handling state without additional space

Let’s consider a method that takes a numeric string and parses it into a

BigDecimal

while also adjusting its scale:


/**
 * parses string number into BigDecimal with a scale of 2
 */
fun parseNumber(number: String): BigDecimal {
    return number.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun main() {
    println(parseNumber("100.12212"))
}

The code is pretty straightforward and would work just fine, but a requirement could be that you need to keep somehow track of the original string that was used to parse the number. To solve that, you would probably create a wrapper type or maybe use the existing

Pair

class to return a pair of values from that function. Those approaches would be valid although it obviously allocates additional space, which, in particular situation, should be avoided.
Inline classes can help you with that. We already noticed that inline classes can’t have multiple properties with backing fields. However, they can have simple calculated members in the form of properties and functions. We can create an inline class for our use case that wraps the original

String

and provides a method or a property that, on demand, parses our value. For the user, this will look like a normal data wrapper around two types while it does not add any runtime overhead in the best case:


inline class ParsableNumber(val original: String) {
    val parsed: BigDecimal
        get() = original.toBigDecimal().setScale(2, RoundingMode.HALF_UP)
}

fun getParsableNumber(number: String): ParsableNumber {
    return ParsableNumber(number)
}

fun main() {
    val parsableNumber = getParsableNumber("100.12212")
    println(parsableNumber.parsed)
    println(parsableNumber.original)
}

As you can see, the

getParsableNumber

method returns an instance of our

inline

class which provides two properties

original

(the underlying type) and

parsed

(the calculated parsed number). That’s an interesting use case that is worth observing on a byte code level again:

More byte code


public final class ParsableNumber {
   @NotNull
   private final String original;

   @NotNull
   public final String getOriginal() { return this.original; }

   // $FF: synthetic method
   private ParsableNumber(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      super();
      this.original = original;
   }

   @NotNull
   public static final BigDecimal getParsed_impl(String $this) {
      BigDecimal var10000 = (new BigDecimal($this)).setScale(2, RoundingMode.HALF_UP);
      Intrinsics.checkExpressionValueIsNotNull(var10000, "original.toBigDecimal().…(2, RoundingMode.HALF_UP)");
      return var10000;
   }

   @NotNull
   public static String constructor_impl(@NotNull String original) {
      Intrinsics.checkParameterIsNotNull(original, "original");
      return original;
   }

   // $FF: synthetic method
   @NotNull
   public static final ParsableNumber box_impl(@NotNull String v) {
      Intrinsics.checkParameterIsNotNull(v, "v");
      return new ParsableNumber(v);
   }

   // $FF: synthetic method
   @NotNull
   public final String unbox_impl() { return this.original; }

    //more Object related implementations
}

The generated wrapper class

ParsableNumber

pretty much looks like the earlier shown

WrappedInt

class. One important difference, however, is the

getParsed_impl

function, which represents our computable property

parsed

. As you can see, the function is implemented as a static function that takes a string and returns a

BigDecimal

. So how is this being utilized in the caller code?


@NotNull
public static final String getParsableNumber(@NotNull String number) {
    Intrinsics.checkParameterIsNotNull(number, "number");
    return ParsableNumber.constructor_impl(number);
}

public static final void main() {
    String parsableNumber = getParsableNumber("100.12212");
    BigDecimal var1 = ParsableNumber.getParsed_impl(parsableNumber);
    System.out.println(var1);
    System.out.println(parsableNumber);
}

As expected,

getParsableNumber

does not have any reference to our wrapper type. It simply returns the

String

without introducing any new type. In the

main

, we see that the static

getParsed_impl

is used to parse the given

String

into a

BigDecimal

. Again, no usage of

ParsableNumber

.

Reducing the scope of extension functions

A common issue with extension functions is that they may pollute your namespace if defined on general types like

String

. As an example, you may want to have an extension function that converts a JSON string into a corresponding type:


inline fun <reified T> String.asJson() = jacksonObjectMapper().readValue<T>(this)

To convert a given string into some data holder

JsonData

, you would then do:


val jsonString = """{ "x":200, "y":300 }"""
val data: JsonData = jsonString.asJson()

However, the extension function is available on strings that represent other data as well although it might not make much sense:


"whatever".asJson<JsonData> //will fail

This code will fail since the

String

does not contain valid JSON data. What can we do to make the extension shown above only available for certain strings? Yep, inline classes can help with that:

Narrow down extension scope with inline class


inline class JsonString(val value: String)
inline fun <reified T> JsonString.asJson() = jacksonObjectMapper().readValue<T>(this.value)

When we introduce a wrapper for strings that holds JSON data and change the extension to using a

JsonString

receiver accordingly, the issue described above has been solved. The extension won’t anymore appear on any arbitrary

String

but only those we consciously wrapped in a

JsonString

.

Unsigned Types

Another great use case of inline classes becomes apparent when looking at the unsigned integer types that were added to the language with version 1.3, also as an experimental feature:


public inline class UInt @PublishedApi internal constructor(@PublishedApi internal val data: Int) : Comparable<UInt>

As you can see, the

UInt

class is defined as an unsigned class that wraps a normal signed integer

data

. You can learn more about this feature in the corresponding KEEP.

Conclusion

Inline classes are a great tool that can be used to reduce heap allocations for wrapper types and helps us with solving different kinds of problems. However, be aware that certain scenarios such as using inline classes as nullable types require boxing. Since inline classes are still experimental, you have to expect your code to break in future releases due to changes in their behavior. Please keep this in mind. Nevertheless, I think it’s valid to start using them right away.

If you want to learn more about Kotlin’s beautiful features I recommend the book Kotlin in Action to you and also like to direct you to my other articles 🙂

Another great article about inline classes can be found here (by Dave Leeds).

The post Kotlin Inline Classes – How they work and when you should use them appeared first on Kotlin Expertise Blog.

Continue Reading Kotlin Inline Classes – How they work and when you should use them

End of content

No more pages to load