avatarGabriel Shanahan

Summary

The website content provides an explanation of the also and apply scope functions in Kotlin, emphasizing their practical uses, differences, and best practices for their application in code.

Abstract

The article delves into the Kotlin programming language's scope functions, specifically also and apply. It describes also as a function that allows for the execution of side-effects within a block of code that operates on a receiver object, without altering the object itself. The apply function is presented as similar to also, but it uses the receiver object as the context for the block (this), allowing for more concise property assignments to the object. The article illustrates these concepts with Kotlin Playground examples and discusses the importance of code readability over brevity. It also suggests that understanding when to use also versus apply is crucial for writing elegant and maintainable Kotlin code.

Opinions

  • The author suggests that while also and apply are similar, their subtle differences can lead to more elegant code in certain situations.
  • It is emphasized that also can be used for logging or other side-effects without the need to declare extra variables, thus potentially leading to more concise code.
  • The article posits that apply is particularly useful for setting up objects with multiple properties in a clear and succinct manner.
  • The author advises that the choice between also and apply should be guided by the principle of prioritizing readability over shorter or more concise code.
  • The preference for also or apply is context-dependent, and developers should consider the specific use case when deciding which scope function to employ.
  • The article implies that learning the nuances of Kotlin's scope functions is part of a broader initiative to facilitate Kotlin adoption within Java-centric organizations.

Scope Functions: also() & apply()

A description of the also() and apply() scope functions in Kotlin.

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FUNDAMENTAL CONCEPT

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Also

The definition of also is essentially this:

If this seems familiar, it is because you wrote your own version in a previous lesson.

From a purely practical standpoint, what this allows you to do is to execute a side-effect (i.e. a block of code that does not return anything = Unit) and then continue working with the receiver.

We talked about one possible use, which was logging information inside a longer call chain:

You could easily get by just by declaring an extra variable:

However, not only does also save you some keystrokes, but in some situations can lead to more elegant code by compressing it into a single expression. That can come in handy when defining a function as an expression.

But don't forget - shorter/more concise is not always better! Prioritize readability over everything else.

Apply

The definition of apply is essentially:

When you compare apply with also, you'll come to the startling realization that they are practically identical - the only difference is that also uses the receiver as a parameter to block, while apply uses it as a receiver.

From a functional standpoint, the following are completely equivalent:

So when should you use which, and more importantly, why? Read on to find out.

Go back to Scope Functions: Introduction, jump to the Table of Contents, or continue to Scope Functions: also() vs. apply().

Join me in Etnetera
Java
Kotlin
Programming
Functional Programming
Kotlin Scope Functions
Recommended from ReadMedium