tezvyn:

What is a suspend function in Kotlin and its compiler rules?

AI-drafted, machine-checkedSource: kotlinlang.orgbeginner
What is a suspend function in Kotlin and its compiler rules?

This tests your understanding of suspendable computations and compiler transformation. A strong answer notes non-blocking suspension, the call-from-suspend-context rule, and compiler support for pausing and resuming. A red flag is claiming they block threads.

WHAT THIS TESTS: This question checks whether you understand the distinction between blocking and suspending code, the language rules that govern where suspend functions can be called, and the high-level compiler mechanism that makes non-blocking suspension possible. Interviewers want to see that you know suspend is a core language feature, not just a library annotation, and that you can explain why it matters for thread utilization.

A GOOD ANSWER COVERS: First, define a suspend function as a suspendable computation that can pause its execution and resume later without blocking the underlying thread. Second, state the main call rule: you can only invoke a suspend function from another suspend function or from a coroutine builder like launch or async. Third, note that the entry point for a Kotlin application can be marked suspend fun main, or you can bridge from regular code using runBlocking. Fourth, explain that under the hood the compiler uses the suspend keyword to transform the function so the coroutine runtime can pause and resume it, allowing the underlying thread to be reused while waiting. Fifth, clarify that the suspend keyword is built into the language, while most concrete coroutine APIs like delay, withContext, and Dispatchers come from the kotlinx.coroutines library.

COMMON WRONG ANSWERS: A major red flag is claiming that suspend functions block the thread or are just syntactic sugar for threads. Another is saying you can call a suspend function anywhere without a coroutine context. Some candidates confuse suspend with async or assume it automatically runs code in parallel. Treating suspend as a simple marker with no runtime effect also signals shallow understanding.

LIKELY FOLLOW-UPS: The interviewer may ask how delay differs from a blocking sleep call, when to use runBlocking versus suspend fun main, or how structured concurrency works with CoroutineScope. They might also dig into the difference between suspend and blocking IO, or ask how exception handling differs in suspend functions compared to regular functions.

ONE CONCRETE EXAMPLE: Consider a suspend fun fetchUser that calls delay to simulate a network request. Inside a withContext block using Dispatchers.Default, fetchUser can suspend without blocking the thread, allowing other coroutines on the same dispatcher to run. When the delay completes, the function resumes and the next line executes as if the code were sequential, even though the underlying thread may have been reused for other work in the meantime.

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.