https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

df

11/24/2023, 1:42 AM
Hi all. Is there a specific reason why the initial assigment of a property does not call its setter?
Copy code
var discount: Discount = discount.also { it.constraints = this }
        set(value) {
            field = value
            field.constraints = this
        }
Somehow I would like to avoid the duplicated code.
e

ephemient

11/24/2023, 3:56 AM
patterns like
Copy code
var x
    set(value) {
        if (value != field) notifyChange(field, value)
        field = value
    }
val changes = mutableListOf()
fun notifyChange(old, new) {
   changes.add(old to new)
would be problematic as both
field
and the rest of the class are not yet initialized
there are already enough ways to violate nullability constraints during initialization in Kotlin, we don't need more
d

df

11/24/2023, 12:08 PM
Makes sense. Thank you 🙂
2 Views