how do I stick to keeping each class member immuta...
# getting-started
o
how do I stick to keeping each class member immutable and still change the fields and modify them as I please?
k
You can use the builder pattern in conjuction with lambdas as follows.
Copy code
data class Address(val street: String, val suburb: String)

class AddressBuilder {
    var street: String = ""
    var suburb: String = ""

    fun build() = Address(street, suburb)
} 

fun address(block: AddressBuilder.() -> Unit) {
        address = AddressBuilder().apply(block).build()
}
and at the call site you can do this
Copy code
address {
     street = "23 Kotlin Ave"
     suburb = "Yorkville"
}
j
I thought you can use the
copy
method to create a new instance with updated value.