Why doesn't state behave like the documentation says?

Well, that’s the ingenious/tricky thing. state.update returns content, but what it returns is not visible. Think of it as an invisible token or cookie that represents the state update.

What Typst is trying to tell you is that state.update returns a sliver of content that you must include in the document’s content otherwise the state update won’t happen at all. Updates happen in the order and location that these “cookies” appear in the document…

I think you seem to have a handle on it. Maybe only that the final expected value should be 5 because before the final state you have done both +1 and +2 to the state, so the final value should be 2 + 1 + 2 = 5

My state explanation would sound like this: Using state for Caching in Typst — Is It Possible? - #3 by bluss


The next thing you can try is making an update but not inserting it, it will not affect the final count:

#let my-update = st.update(c => c + 1)

Then insert your update twice, and it will affect the final count (twice):

#my-update #my-update
4 Likes