August 2017
Intermediate to advanced
440 pages
10h
English
Since Kotlin 1.1, the inline modifier can be used on properties that do not have a backing field. It can be either applied to separate accessors, which will result in their body replacing usage, or it can be used for a whole property, which will have the same result as making both accessors inline. Let's make an inline property that will be used to check and change an element's visibility. Here is an implementation where both accessors are inline:
var viewIsVisible: Boolean
inline get() = findViewById(R.id.view).visibility == View.VISIBLE
inline set(value) {
findViewById(R.id.view).visibility = if (value) View.VISIBLE else View.GONE
}
We can achieve the same result if we annotate the whole property as inline:
inline var ...
Read now
Unlock full access