What is a Kotlin extension function? Write one for String.

Tests adding functionality to classes without inheritance. Define extension functions as static utilities, explain their use case (e.g., third-party libs), then write `hasWhitespace` using an idiomatic method like `any`. A red flag is manually looping.
This tests your deep understanding of Kotlin's extension functions: the 'why' and the 'how'. A great answer first defines them as a way to add functionality to a class without inheritance, noting they are resolved statically. Explain their utility for non-modifiable classes (like from SDKs). Finally, provide a concise, idiomatic implementation for `hasWhitespace` using the `any` function. A red flag is using a verbose `for` loop or suggesting extensions modify the original class.
Read the original → kotlinlang.org
- #kotlin
- #android
- #extensions
- #idiomatic code
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.