Inline Properties

If you implement a property with custom accessors that do not reference the backing field, those accessors can be marked as inline:

val stuff = mutableMapOf<String, String?>("something" to "This is the 'something' value")
var something: String?
  inline get() = stuff["something"]
  inline set(value) { stuff["something"] = value }

fun main() {
  something = "This is different"
  println(something)
}

And, if you want both accessors to be inline — as shown above — you can mark the whole property as inline:

val stuff = mutableMapOf<String, String?>("something" to "This is the 'something' value")
inline var something: String?
  get() = stuff["something"]
  set(value) { stuff["something"] = value }

fun main() {
  something = "This is different"
  println(something)
}

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.