在Kotlin中,如何定义具有延迟默认值的var
?
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?
编辑:这是部分技巧.
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 lazily? At the moment it won't work because color()
needs a Context
that is only initialized later.