Posts

Showing posts from July, 2017

Intention of Kotlin's "also apply let run with"

Intention of Kotlin's "also apply let run with"One of the things that puzzled me when I started with Kotlin was why are there so many similar functions which call lambda on some objects and returns a value. After many lines of code and many lines of user group discussions, I found out that they represent a small DSL for easier with classes. Explanation of this DSL and intent of each function is missing from the Kotlin documentation, so this article will hopefully shed some light on it. There is also a short style guide on GitHub.alsoWith this function, you say “also do this with the object”. I often use it to add debugging to the call chains or to do some additional processing:kittenRest .let{KittenEntity(it.name, it.cuteness)}.also{println(it.name)}.also{ kittenCollection += it }.let{ it.id }also passes object as parameter and returns the same object (not the result of the lambda!):val text ="How is it going?" text.also{s -> s.toUpperCase()} How is it …