Easter functions in Kotlin
Akshay Chordiya
2065

Just wanted to mention that the snippet provided as example of with function:

val person = with(Person()) { name = “Tony Stark” age = 52 // More such stuff}

It may lead to think that this block returns a Person instance, but, as is, val person will be of type Unit because with function takes the receiver as parameter and return the function result, which in this case it is inferred to Unit. To make with function to return a Person instance you need to return this in the block, in which case is better to use apply function, that always returns its caller.

Like what you read? Give Álvaro Blanco Cabrero a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.