Is it possible to have an inline block of code tha...
# announcements
u
Is it possible to have an inline block of code that returns an object. In the example below you have to “manually” call
invoke()
. Is there any other way to achieve this?
d
Usually you can use
run
for this:
Copy code
val users = listOf(
    run {
        val user = User()
        user.id = 1
        user
    }
)
👍 1
But in this case you can even simpler use `also`:
Copy code
val users = listOf(
  User().also { it.id = 1 }
)
K 1
w
Can use
apply
to "initialise"
Copy code
val users = listOf(
                User().apply { id = 1 }
        )
K 1
u
Thank 🙂