CoursesCompose Multiplatform, one screen from state to pixels
Puzzle 0 · 1 of 11
What @Composable really does
The KMP course said a compiler plugin adds "hidden bookkeeping" to every composable. Here's what it really adds: a hidden parameter that every composable hands on to the next, and a start and an end around every call.
All puzzles in this course
The problem
The KMP course ended its Compose puzzle with one sentence: a compiler plugin rewrites every @Composable function with "hidden bookkeeping". Rewrites it into what?
Look at Greeting. It takes a name, calls Text, and returns Unit. A plain Kotlin function forgets everything the moment it returns. Yet Compose can later run this one call again on its own, and it knows what the call showed last time. And if you call Greeting from an onClick lambda, the build fails. So something is passed into every composable that you never wrote.
See it
Scroll sideways to see the full diagram.
What you write:
Greetingtakes a name and callsText. A plain Kotlin function forgets everything the moment it returns.Step 1 of 5The compiler plugin adds parameters you never wrote.
$composeris the Composer: the runtime's recorder, handed into every composable call.Step 2 of 5It also wraps the body in a start and an end. What the call records in between is its group. Its key comes from Greeting's name and parameters, so every call to Greeting opens a group with the same key.
Step 3 of 5Columnhands its$composertoGreeting, which hands it toText: one Composer travels down every call. Predict: can anonClicklambda callGreeting?Step 4 of 5No. An
onClicklambda isn't a composable, so it has no$composerto hand on, and the compiler refuses the call.Step 5 of 5
The building block
@Composable is an annotation, but it does more than label the function: the Compose compiler plugin rewrites every function that carries it. Since Kotlin 2.0.0 the plugin's code lives with the Kotlin compiler's own code and ships with every Kotlin release, at the same version; your build turns it on with the Gradle plugin org.jetbrains.kotlin.plugin.compose. Roughly (simplified from the comments in the plugin's own source code):
// What you write
@Composable
fun Greeting(name: String) {
Text("Hello, $name")
}
// Roughly what the plugin turns it into
@Composable
fun Greeting(name: String, $composer: Composer, $changed: Int) {
$composer.startRestartGroup(1234) // Greeting's key, made from its name and parameters
Text("Hello, $name", $composer, /* … */) // the composer is handed on
$composer.endRestartGroup()
}
Three things changed.
A hidden parameter. $composer is the Composer, an object from the Compose runtime. Every composable call records what it did through it: which composables it called, in which order, with which arguments, and which values it asked to keep. You never call it yourself: the runtime's code says the Composer is "targeted by the Compose Kotlin compiler plugin", so it trusts that only generated code uses it.
A start and an end. startRestartGroup opens the call's record and endRestartGroup closes it. Everything one call records between its start and its end belongs to that call: its group. Groups nest the way calls nest: Text is a composable too, so it opens a group of its own, inside Greeting's.
Each group carries a key, a number the plugin computes while it compiles. For a function's own group, the key comes from the function's name and parameter types: every call to Greeting, wherever it sits, opens a group with the same key. (Groups the plugin adds inside a body, such as one around an if, get keys from their position in the source instead; puzzle 1 uses them.) So the key says which function recorded a group, not which call. Two Text calls side by side open two groups with the same key; puzzle 1 shows how the runtime still tells them apart.
The word restart in the name is a promise: Compose can later run this group again, on its own, from its start. Puzzle 3 shows when, and why that needs the group.
A second hidden parameter, $changed: an Int in which the caller passes on what it already knows about each argument, such as "this is the same value as last time". Puzzle 4 shows what it's for.
Then the rule you've met as a compile error. Inside Greeting, the call to Text gets Greeting's own $composer. So one Composer travels down through every composable call, from where Compose starts (on Android, setContent { }; on iOS, the lambda you pass to ComposeUIViewController) to the smallest Text. A plain function, such as an onClick lambda, has no $composer to hand on, so the compiler refuses the call. That's what "a composable can only be called from another composable" means. It's the same shape as suspend: you can only call a suspending function from another suspending function.
Go deeperDoes a function's key change when you move it?
You add ten blank lines above Greeting and build again. Does the key in startRestartGroup change?
No. The plugin's key builder hashes a text made from the function's name and parameter types ("fun-" followed by them), not its line or offset. Its own comment says the key is "durable": "a function with the same semantic identity will usually have the same key each time it is compiled". That lets tools that swap in edited code while the app runs tell the runtime exactly which functions' groups are out of date. Only the groups inside a body, around an if for example, use positions, and those positions are counted from the start of the function, so moving the whole function doesn't change them either.
The analogy
The theatre from the KMP course, where the same play is staged in two theatres. You, the playwright, write the scenes. Before rehearsals, the script editor (the compiler plugin) marks the start and the end of every scene and adds one line to each: "hand the prompt book to the next scene". The stage manager's prompt book (the Composer) travels through the whole play, and each scene notes what it did between its marks (its group). A scribble on a napkin (a plain lambda) has no marks, and no prompt book to hand on.
Check yourself
Sources
- Compose runtime source: Composable.kt ("Annotating a function or expression with [Composable] changes the type of that function or expression. For example, [Composable] functions can only ever be called from within another [Composable] function. A useful mental model for [Composable] functions is that an implicit "composable context" is passed into a [Composable] function")
- Compose runtime source: Composer.kt ("Composer is the interface that is targeted by the Compose Kotlin compiler plugin and used by code generation helpers. It is highly recommended that direct calls these be avoided as the runtime assumes that the calls are generated by the compiler")
- Compose compiler plugin source: ComposableFunctionBodyTransformer.kt ("Generally speaking, every composable function must emit a single group when it executes. Every group can have any number of children groups."; its example rewrite: "@Composable fun A(x: Int, $composer: Composer<>, $changed: Int) { $composer.startRestartGroup() … $composer.endRestartGroup()"; the function's own group gets "irStartRestartGroup(body, scope, irFunctionSourceKey())", while groups inside a body use sourceKey(): "hash = 31 hash + (startOffset - currentFunctionScope.function.startOffset)")
- Compose compiler plugin source: DurableFunctionKeyTransformer.kt ("This transform will generate a "durable" and mostly unique key for every function in the module."; "a function with the same semantic identity will usually have the same key each time it is compiled"; the key is built from "fun-$signature"; and in AbstractComposeLowering.kt,
IrSimpleFunction.sourceKey()returns that durable key) - Kotlin docs: Compose compiler migration guide ("The Compose compiler has been merged into the Kotlin repository since Kotlin 2.0.0"; "the Compose compiler ships simultaneously with Kotlin and will always be compatible with Kotlin of the same version"; the Gradle plugin entry:
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }) - Android Developers: Thinking in Compose ("This annotation informs the Compose compiler that this function is intended to convert data into UI.")
- Kotlin docs: Coroutines basics ("You can only call a suspending function from another suspending function.")
What you now know
The Compose compiler plugin rewrites every @Composable function. It adds a hidden $composer parameter (and a $changed one), and wraps the body in a start and an end, so everything the call records in between forms its group. The group's key comes from the function's name and parameters, so every call to the same function opens a group with the same key. Each composable passes its Composer on to the composables it calls, which is why only a composable can call a composable.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.