Kotlin lazy default property


#1

In Kotlin, how do i define a var that has a lazy default value ?

for example, a val would be something like this:

val toolbarColor by lazy {color(R.color.colorPrimary)}
What i want to do is, have a default value for some property (toolbarColor), and i can change that value for anything else. Is it possible?

EDIT: This does the partial trick.

var toolbarColor = R.color.colorPrimary
get() = color(field)
set(value){
field = value
}
Is it possible to ease this by writing

var toolbarColor = color(R.color.colorPrimary)
set(value){
field = value
}
in a way that the default value is computed lazyly? Atm it won’t work because color() needs a context that is only initialized later.


#2

I don’t think such a property delegate is provided out-of-the-box, but you can always implement your own delegate.