Inline Classes
With Kotlin 1.3, inline
isn’t just for functions anymore!
What?
In the preceding chapter, we saw inline
applied to functions. This says “instead of making the actual function call, put the code for the function ‘inline’ where the call is being made”.
Kotlin 1.3 added support for inline
applied to classes:
inline class InvoiceLineItemKey(val key: String)
An inline class
is only allowed to have one property, and it must appear in the constructor. They are allowed to have functions and properties with custom accessors (but no backing fields).
When the code is compiled — assuming that there are no compile errors — all occurrences of this inline class
are replaced by references to the one-and-only property of that class, and all references to function calls are replaced with inline representations of those functions.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.