The apply() function

The apply() function simply executes the function or given block with its own reference this as the receiver and returns the same updated object.

A typical case of using apply can be:

    supportActionBar?.apply {      setDisplayHomeAsUpEnabled(true)      setDisplayShowHomeEnabled(true)    }

Which is as good as writing:

    if(supportActionBar != null){      supportActionBar.setDisplayHomeAsUpEnabled(true);      supportActionBar.setDisplayShowHomeEnabled(true);    }

Get Kotlin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.