Inline value classes
Good wrap up article (with great example of the usage)
Thoughts from official Kotlin team
We can use inline value classes to make a wrapper around some type with ❗ no performance overhead (Item 47: Avoid unnecessary object creation). Two especially popular uses of inline value classes are:
To indicate a unit of measure.
To use types to protect users from value misuse.
JPA Entities
In SQL databases, we often identify elements by their IDs, which are all just numbers. For instance, let’s say that you have a student’s grade in a system, which will probably need to reference the id of a student, teacher, school, etc:
The problem is that it is really easy to later misuse all these ids, and the type system does not protect us because they are all of type Int
. The solution is to wrap all these integers into separate inline value classes:
Now those id uses will be safe and the database will be generated correctly because all these types will be replaced with Int
anyway during compilation. This way, inline value classes allow us to introduce types where they were not allowed before; thanks to this, we have safer code with no performance overhead.
Last updated
Was this helpful?