I often come across a situation where i fetch some...
# announcements
a
I often come across a situation where i fetch some data class, process a field, then return the result. A contrived example:
Copy code
fun getFilm(title: String): Film {
    val data = db.getFilm(title)
    return data.copy(actors = data.actors.filterNot { it.isDead) })
}
Is there a way to avoid the temp variable without hitting the db twice? Or put differently, is there a way of referencing the source object in a copy operation somehow? Ideally I'm looking for something like
fun getFilm(t: String) = db.getFilm(t).copy(actors = actors.filterNot { it.isDead })
I'm aware of
with
, but in cases like this,
with
feels even more convoluted.
t
You should use apply in this case. That returns the object U call apply on basically. Run, also, apply, let has all their use-cases.