Working with Kotlin Inline class

Anvith Bhat
Sep 1, 2018 · 3 min read

Recently Kotlin 1.3 introduced Inline classes which add the goodness of Typealiases with the value range of the primitive data types. Let’s try to dig into how to use them.

Lets say you have an inventory of items for sale and each of them has a Floating cost. So you start of with this data class

data class Item(val id: Int, val price: Float, val qty: Int)

Since you support twin currencies (₹ and $), you aren’t sure which is the value you need to set while using this class. So you refactor price into its own class.

data class Item(val id: Int, val price: Price, val qty: Int)

data class Price(val value: Float, val currency: Currency)

enum class Currency {
RUPEE,
DOLLAR
}

Now adding these two extra classes incurs a memory overhead. Wouldn’t it be great if we got the “type descriptor” functionality of the currency enum and the memory overhead of just the raw float denoting the price?
Enter Inline classes …

data class Item(val id: Int, val price: RupeePrice, val qty: Int)

inline class RupeePrice(val price: Float) {

inline fun toDollars(): Float = price * 70.0f
}

Now essentially the above inline class has 2 parts

  • The underlying value variable: price
  • The datatype of the underlying variable: Float

Advantages of having inline classes are two-fold

1) Developer ease : One can easily qualify the datatype by a descriptive inline class name and not worry about accidentally putting a wrong value i.e substitute value in rupees for dollars or vice versa while creating instances of the class.

2) Memory Overhead: The runtime never sees the inline class all it sees is the underlying value since the compiler does the dirty work here, so this

val price = RupeePrice(100f)

is basically seen by the run time as

val price = 100f

Can’t we do the same with typealiases?

Type aliases allow cross assigning the different type aliased values if the underlying datatype is the same, this could result in erroneous assignments. Inline classes are more stringent since the value is essentially wrapped and accessing is more explicit as we need to use the .value accessor on the instance.

A few GOTCHA’s about Inline classes

- They must contain only one constructor parameter.
- You can’t extend an(or from) inline class.
- The syntax mandates a primary constructor with an immutable(val)
parameter.
- Inline classes cannot have init{} block since this creates a problem while inline the underlying value while interoperating with Java since the init block cannot be passed around.

That’s it folks

Interested in reading more on Inline classes and their behaviours? Check out the KEEP file.

Anvith Bhat

Written by

Android code pays for my sandwiches.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade