typst / typst

A new markup-based typesetting system that is powerful and easy to learn.
https://typst.app
Apache License 2.0
34.83k stars 928 forks source link

More detailed documentation on the behavior of multiple `state` calls with the same key #2680

Open yaksher opened 12 months ago

yaksher commented 12 months ago

The current documentation of State is very ambiguous on how exactly multiple calls to state with the same key behave.

image

My testing suggests that:

This means that calls to update don't care which handle you use for them, only the key of that handle, while calls to display() and similar will all depend on which handle you use. For example, the below code displays:

#let handle1 = state("test", 1)
#let handle2 = state("test", 2)
#handle1.display()
#handle2.display()
#handle1.update(it => it + 10)
#handle2.update(it => it + 10)
#handle1.display()
#handle2.display()
#state("test").update(3)
#handle1.display()

1, 2, 21, 22, 3 (note that both updates applied to the state accessed through both handles, but even after updating, the values are distinct)

I'm not sure how best to summarize this, but it should probably be somewhere in the docs.

laurmaedje commented 12 months ago

Your description is accurate.