I am new to Kotlin and just started learning it. I came across with
and apply
functions in Kotlin. I just want to understand the difference between them in the below scenario:
I have a class Person
and I assigning values to the properties of its object like this:
val andre = Person().apply {
name = "Andre"
company = "Viacom"
hobby = "losing in ping pong"
}
But this can be done using the with
function as well:
val andre = Person()
with(andre){
name = "Andre"
company = "Viacom"
hobby = "losing in ping pong"
}
Now what is the difference between with
and apply
functions in this context ?