To add to my previous reply. My own examples show one of the pitfalls of scope operators:

openFile(name)

?.run { processFile(this) }

?: handleFileError(name)

The elvis expression is executed if the previous operator returns null so if the expression in the run lambda returns null, it would execute both processFile and handleFileError. The correct code is:

openFile(name)

?.apply { processFile(this) }

?: handleFileError(name)

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Emanuel Moecklin

Emanuel Moecklin

More from Medium

GSoC 2015 — Moorsp Plagiarism Plugin for Moodle

CS371p Spring 2022: Fazal Ali

My experience with Rupee

Android Interview Cheat Sheet. Part 2: Activity and Fragment Lifecycle Questions