Eliminating backing property type in Kotlin 1.7

Nikola Despotoski
2 min readApr 4, 2022

--

At one KotlinConf, I don’t quite recall which year it was, overriding backing properties type (eliminating the backing prop completely) was most wanted language feature along side with contextual receiver extension functions.

It is certain that every kotlin codebase now has following, one private property that is mutable and one immutable backed by the mutable, that is exposed for consuming:

In Kotlin 1.7, this will be history, and we will be able to override property getter type. This language feature will enable having one property for updating and read-only operations.

I assume the compile sees that the property is private but the getter is explicitly set as public and generates getter function with the immutable type. This does not interferes with private getter which probably uses the local context and does not mix up the public getter.

In current Kotlin version this is conflicting visibility and getter must follow property declared visibility.

Personally, I can’t wait to start using this feature!

Follow full discussion here:

https://youtrack.jetbrains.com/issue/KT-14663

Update (11.04.2022):
Thank you Matthew Dolan for linking the update readme on backing property proposal:
https://github.com/Kotlin/KEEP/blob/explicit-backing-fields-re/proposals/explicit-backing-fields.md

--

--

Nikola Despotoski
Nikola Despotoski

Written by Nikola Despotoski

Knitting code. One line at a time. @ WS Audiology

Responses (2)