Examples of using run, let, apply, also and with in Kotlin: when to use what?
Hello colleagues! I started learning Kotlin and I can't figure out the extension functions run, let, apply, also and with. The documentation says they help write cleaner code, but I constantly get confused about which one to choose.
For example, I have a simple User class:
```kotlin
data class User(var name: String, var age: Int, var email: String)
```
And I want to create an instance, modify its fields, and then output it to the log. I tried different options:
```kotlin
// Option 1
val user = User("", 0, "")
user.name = "Ivan"
user.age = 25
user.email = "ivan@mail.ru"
println(user)
// Option 2
val user = User("", 0, "").apply {
name = "Ivan"
age = 25
email = "ivan@mail.ru"
}
println(user)
```
It seems like apply works, but when I need to return a result p
No answers yet.
Log in to leave an answer