tezvyn:

What is an extension function? Write a hasWhitespace extension for String.

Curated by the Tezvyn teamSource: kotlinlang.orgintermediate
What is an extension function? Write a hasWhitespace extension for String.
WHAT IT TESTS

Kotlin extensions are static dispatch, not true members.

ANSWER OUTLINE

Explain receiverType.functionName adds behavior without inheritance; write hasWhitespace with any { it.isWhitespace() }.

RED FLAG

Extensions modify classes or override.

WHAT THIS TESTS: This question probes whether you understand that Kotlin extension functions are syntactic sugar for static utility methods rather than true class members. The interviewer cares if you know the dispatch model, scoping rules, and receiver syntax, and whether you can write a concise, idiomatic implementation instead of a verbose Java-style helper.

A GOOD ANSWER COVERS: A strong response hits four things in order. First, define an extension function as a way to add new behavior to an existing class or interface without inheritance, decorators, or modifying the original source. Second, explain the syntax: prefix the function name with the receiver type and a dot, as in fun String.hasWhitespace(). Third, clarify static dispatch: extensions are resolved at compile time based on the declared type of the receiver, not the runtime type, so they cannot be overridden polymorphically and do not appear in the class bytecode as real members. Fourth, provide the implementation using the receiver this implicitly or explicitly, such as fun String.hasWhitespace(): Boolean = this.any { it.isWhitespace() }, and mention that any is a standard library extension already available on CharSequence.

COMMON WRONG ANSWERS: The biggest red flag is saying the extension modifies the String class or that it becomes a real member available to Java callers as an instance method. Another mistake is claiming extensions participate in inheritance or can override a superclass method. Some candidates write imperative loops or manual regex instead of any, which works but misses idiomatic Kotlin. A subtle error is forgetting the return type or using contains(" ") which only catches a space and not tabs or newlines.

LIKELY FOLLOW-UPS: The interviewer may ask how extensions interact with Java interop, specifically that they compile to static methods in a generated class file taking the receiver as the first argument. They may ask about nullable receivers, extension versus member function precedence when both exist, or how to scope an extension to a specific file or package. Another follow-up is asking for a generic extension, such as fun T.log(), to test generic type parameter placement.

ONE CONCRETE EXAMPLE: If you call "Hello World".hasWhitespace(), the compiler desugars it to something like StringExtensionsKt.hasWhitespace("Hello World") in the bytecode. Inside the function, this refers to "Hello World". If you had a local variable val s: String? = "A", you could define fun String?.hasWhitespace(): Boolean = this?.any { it.isWhitespace() } ?: false, showing a nullable receiver.

Source: kotlinlang.org

Read the original → kotlinlang.org

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.

What is an extension function? Write a hasWhitespace extension for String. · Tezvyn