Feedback on LazyConstant (previously StableValue)

Mikhail Vorobev mikhail.vorobev at jetbrains.com
Mon Nov 10 15:51:22 UTC 2025


Hello Amber Team,

I write to provide a data point from the Kotlin Evolution Team on the
design of LazyConstant (previously StableValue). In Kotlin, there is a
`lateinit var` declaration for a variable that is in an uninitialized state
until it is assigned later during execution, after that it behaves like a
normal variable. In practice, people tend to use such declarations
primarily for variables with stable semantics, e.g. for injected
dependencies or Android View bindings:

    class Application {

        @Inject lateinit var service: Service

    }

    class MyActivity : AppCompatActivity() {

        lateinit var view: ImageView

        override fun onCreate(...) {

            view = findViewById(R.id.image)

        }

    }

These variables are intended to be initialized at most once and never
change after that. To cater to such use cases, we considered introducing
`lateinit val` declaration in some form (possibly just as a delegate,
similar to how lazy is implemented now in Kotlin).

StableValue was a perfect fit for the implementation of `lateinit val`. On
the other hand, LazyConstant could not be used to express desired semantics
(without hacks) due to its limited API: it does not allow setting the value
directly after construction.

>From our point of view, the current LazyConstant API, in contrast to
StableValue, does not cover a considerable part of use cases for variables
with stable semantics where they are initialized from values available
later and there is no sensible initializer function that could be provided
beforehand.

Best Regards, Mikhail Vorobev, Kotlin Evolution Team
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20251110/ad939b09/attachment-0001.htm>


More information about the amber-dev mailing list