Unwrapped inline class compiler check

I really love to leverage the compiler to explicitly verify my intent. This is great for example with the tailrec modifier.

With inline classes, the compiler is smart enough to figure out when to use a wrapper and when not to, and this is a productivity booster in many cases. However, when using inline classes for performance reasons, I want the compiler to help me detect and change these usages.

I haven’t thought much about it, but two ideas come to mind:

  1. An @Unwrapped annotation
  2. An unwrapped inline class declaration

Thanks, and keep innovating!

How much faster is it?

1 Like

You got me there: I have no measurements to support this request. But hear me out! :slight_smile:

I work on a Java project and I’m exploring Kotlin on my free time, evaluating different aspects to make it appealing to my boss and coworkers.

We work quite a lot with integral types, and I get a lot of pushback when creating abstractions for type safety. Inline classes as they are already offer great advantages, but being able to prove that no allocations are made would be an even better selling point.

This is why I compare it with tailrec: you can write a function and expect the compiler to optimize it, but it’s better to enforce it when needed.

So, please, recap.

You use “inline classes for performance reasons”, but you are not measured any benefit.

So this proposal should reduce the allocation, ok.
How much is it reduced?

In Java, you have the int and Integer types giving control over boxing/unboxing/allocation. As we discuss this, I remember that Kotlin’s Int type hides this away, which kind of goes against this idea.

I guess I got carried away by the great compile time support the language has. It would be cool to have more fine grained control over the inline class compilation process, but I am not ready to talk numbers at the moment.

I’ll keep you posted if I run some experiments!

Hi @collheesi,
take a look here

1 Like