Kotlin Multiplatform, from the compiler up · Puzzle 13 · 14 of 16

Compose in one puzzle

The last module shares the UI itself, with Compose. If you've built screens with Activities and XML, here's the one idea Compose is built on, and the compiler step that makes it work, before we look at what can be shared.

The problem

With XML layouts, you build a view once, then change it by hand: textView.text = name. Compose has no such step: you write a function, and the screen follows your data. How does the screen update, if nothing tells it to?

See it

A composable function Greeting takes a name and describes a text; the screen shows Hello Ana; the name changes to Ben; Compose calls Greeting again and the screen shows Hello Ben; the compiler plugin added the bookkeeping that makes this possible @Composable Greeting(name)Text("Hello, $name") screen Hello, Ana state: Ben Hello, Benrecomposition: called again compiler plugin's bookkeeping
  1. A composable: a function that describes the UI for its data. For "Ana", the screen shows "Hello, Ana".

    Step 1 of 4
  2. The name changes to "Ben". It's state, kept across calls with remember.

    Step 2 of 4
  3. Compose calls Greeting again with "Ben": recomposition. Predict: how does it know what to redo?

    Step 3 of 4
  4. The Compose compiler plugin rewrote the function with hidden bookkeeping, so the runtime redoes only what changed.

    Step 4 of 4

The building block

A composable is a Kotlin function marked @Composable that takes data and describes the UI for it:

@Composable
fun Greeting(name: String) {
    Text("Hello, $name")
}

It returns nothing and creates no view objects you keep. It describes what the screen should look like for this name.

When the data changes, Compose calls the function again with the new value: recomposition. It's smart about it: only the functions whose inputs might have changed run again; the rest are skipped. Data that should trigger it is state, for example var count by remember { mutableStateOf(0) }; remember keeps that value across recompositions, so it isn't reset each time the function runs.

How can the runtime know which functions showed what? That's the Compose compiler plugin, a part of the Kotlin compiler from puzzle 0. It rewrites every @Composable function, adding hidden bookkeeping so Compose can track what each call showed and skip what didn't change.

The analogy

A weather board that a printer reprints whenever the forecast changes, instead of a person climbing up to swap single letters. You only ever write what the board should say; the printer handles the rest, and only reprints the panels that changed.

Check yourself

The name passed to Greeting changes. What makes the text update?

Right. Data changes, functions run again.

Not quite. Only the affected functions run again; nothing is recreated.

Not quite. You keep no Text object to set; you describe, Compose redraws.

Why does a counter need remember?

Not quite. remember is about keeping the value across calls, not visibility. Re-read that part ↑

Right. Kept across calls.

Not quite. remember lasts across recompositions, not after the app is killed. Re-read that part ↑

What's the difference between a composable and the Compose compiler plugin?

Right. What you write vs what the compiler adds.

Not quite. That's the two swapped. Re-read that part ↑

Sources

  • Android Developers: Thinking in Compose ("you can build your user interface by defining a set of composable functions that take in data and emit UI elements"; "This annotation informs the Compose compiler that this function is intended to convert data into UI"; "Recomposition is the process of calling your composable functions again when inputs change. When Compose recomposes based on new inputs, it calls only the functions or lambdas that might have changed, and skips the rest.")

What you now know

In Compose, a screen is a Kotlin function marked @Composable that describes the UI for the current data, instead of creating and updating view objects. When that data, its state, changes, Compose calls the affected functions again: recomposition. remember keeps a value across those calls. A compiler plugin rewrites every @Composable function so the runtime can track what each one showed and redo only what changed.