skip to content

In Jetpack Compose, what does the @Composable annotation change about a function, and where may a composable be called from?

level: juniorimportance: must knowfreq 80%

answer

  1. it changes the function's type
  2. an implicit context travels with the call
  3. only from another composable
  4. onClick is a plain lambda
  5. describe the screen, return Unit

basics

~20 s

@Composable changes a function's type: the Compose compiler passes an implicit composition context into it, so it can only be called from another composable function or composable lambda, never from onClick, a coroutine or an ordinary function.

solid answer

~50 s

`@Composable` is not a marker read at run time. The Compose compiler plugin rewrites the function so an implicit composition context (a `Composer`) travels with every call, and that changes the function's type. So a composable can only be called from another `@Composable` function or from a lambda typed `@Composable () -> Unit`, such as `setContent { }` or a `Column`'s content. `Button`'s `onClick` is a plain `() -> Unit` and an effect's block is a `suspend` lambda, so calling `Text()` in either does not compile; a click changes state, and recomposition calls the composables again. A composable that emits UI returns `Unit` because it describes the screen rather than building widget objects, and it must stay fast, idempotent and free of side effects, because Compose may re-run it, skip it or cancel a pass.

go deeper

for a junior

Recall the rule: a composable is callable only from another composable or a composable lambda, and onClick is not one. Know that UI composables return Unit.

for a middle

Explain why the rule exists: the compiler passes an implicit composition context, which changes the function's type. Tie the side-effect-free style to skipping and cancelled passes.

for a senior

Spot code that leaks work into composition, such as preference reads or writes in a function body, and move it into callbacks or effects. Know that value-returning composables never skip.

for a principal

Treat the contract as something reviews enforce: composables that only describe UI, logic in state holders, and no hidden side effects that break once Compose runs work concurrently.

## What the annotation actually does In Jetpack Compose you build UI from **composable functions**: Kotlin functions annotated with `@Composable` that take data and emit UI by calling other composables. The annotation is not a tag that some framework discovers at run time. It is an instruction to the **Compose compiler plugin**, which ships with Kotlin since Kotlin 2.0 as `org.jetbrains.kotlin.plugin.compose`. The runtime's own documentation puts it this way: annotating a function or expression with `@Composable` **changes the type** of that function or expression. A useful mental model is that an implicit "composable context" is passed into every composable call. That context, the `Composer`, remembers what happened at the same point of the tree on earlier executions. It is how `remember` finds its old value and how Compose knows which part of the UI a call belongs to. ## The calling rule Because the context is passed implicitly, it has to come from somewhere. That gives the one rule interviewers check first: 1. A composable can be called from another `@Composable` function. 2. It can be called from a lambda whose type is composable, for example a parameter typed `@Composable () -> Unit`. 3. It cannot be called from anywhere else, and the compiler rejects the call. The annotation can sit on more than a function declaration. Its targets are functions, lambda expressions (`val header = @Composable { Text("Hi") }`), types (`content: @Composable () -> Unit`), type parameters inside a function type, and property getters (`val accent: Color @Composable get() = ...`). ## Where a composable cannot be called | Call site | Composable? | Why | |---|---|---| | A `Column`'s or `Box`'s trailing lambda | Yes | the parameter is typed `@Composable ...` | | `setContent { }` in an Activity | Yes | it takes a composable lambda | | `Button(onClick = { ... })` | No | `onClick` is `() -> Unit` | | A `LaunchedEffect` block | No | the block is `suspend CoroutineScope.() -> Unit` | | A plain helper function or a ViewModel method | No | no composition context exists there | The `onClick` case is the classic junior mistake: someone wants a dialog after a tap and writes `AlertDialog(...)` inside the click handler. The fix is not to make the handler composable. The handler changes **state**, the composable reads that state, and on the next pass the function calls `AlertDialog` because the state says so. How that state is held and read is covered in the lesson on state and recomposition. ## Describe, do not construct A composable that emits UI **returns `Unit`**. It does not create a widget object and hand it back; it describes what the screen should contain, and Compose turns that description into the tree it keeps, called the **Composition**. There is no `findViewById`, no setter to call later: to change the UI you call the same function again with different arguments. Composables can return values. `remember` does, and so does a getter that reads a theme value. Such a function is legal, but a composable with a non-`Unit` return type is **not eligible for skipping**, so it runs every time its caller runs. Keep value-returning composables small. ## The properties a composable must keep Because Compose decides when and how often to run your function, the documentation asks every composable to be: - **fast**, since it may run as often as every frame of an animation; - **idempotent**, giving the same UI for the same arguments; - **free of side effects**: no writes to shared objects, preferences or a ViewModel's observable from the function body. The reasons are concrete: - Recomposition **skips** whatever it can, so code you expected to run may not. - Recomposition is **optimistic**: if parameters change mid-pass, Compose may cancel the pass and discard its result, while a side effect you started has already happened. - Calls to sibling composables **may run in any order**, so one cannot set a global for another to read. - Compose runs on the main thread today, but it is designed so composables could run in parallel later; a write to a local counter inside a loop is both unsafe and wrong. When you do need a side effect, trigger it from a callback such as `onClick`, or from the effect APIs covered in the lesson on side effects. ## Why interviewers ask it The question separates people who have used Compose from people who have read about it. A strong answer names the compiler as the owner of the contract, states the calling rule with a counter-example (`onClick`, a coroutine), and connects the return-`Unit`, side-effect-free style to the fact that Compose, not your code, decides when a composable runs.

  • In Jetpack Compose, can a @Composable function return a value, and what does that cost?
    Yes. `remember` returns a value, and so does a composable getter that reads a theme value. A composable with a non-`Unit` return type is not eligible for skipping, so it runs every time its caller does. Keep such functions cheap, and keep UI-emitting composables returning `Unit`.
  • Why is incrementing a local counter inside a composable's for loop a bug?
    The increment is a side effect of running composition. Compose may skip parts of the function, cancel and restart a pass, or run sibling calls in any order, so the count shown can be wrong. Derive the value from the input instead, such as `items.size`.
  • How do you show a dialog after a button tap if onClick cannot call a composable?
    Let the click change state, for example a `showDialog` flag, and have the composable call `AlertDialog` when that flag is true. The next recomposition adds the dialog to the UI; dismissing it resets the flag and removes it.

  • The runtime finds @Composable functions by reflection at run time.
  • You can call Text() inside onClick to show a message after a tap.
  • A composable builds and returns a view object you can update later.
  • Composables run top to bottom exactly once per state change.
  • Writing to a shared variable in a composable is safe because it runs on the main thread.