Backing Properties in Kotlin

This is my first post from many years, I hope to be more regular in the future !

Today I will start writing small posts for what I have learned everry day in small examples with my understanding of different languages e.g.
Koltin, Go, and so on and my experience from software development, product development and machine learning.

Kotlin Properties are a different beasts comparing it with the properties in the Java World. That fact make me start investigating Kotlin properties.
Below is an example of “Backing Properties”. It is used when we want to hide setter due to specific initialization or processing for example.

private var _table: Map<String, Int>? = null
public val table: Map<String, Int>
    get() {
        if (_table == null) {
            _table = HashMap() // Type parameters are inferred
        } 
    return _table ?: throw AssertionError("Set to null by another thread") 
}

Backing Propertie are interesting with the following facts :
– implementation of a custom getter;
– private custom member field.

Leave a Comment

Your email address will not be published. Required fields are marked *