|
另一个需要注意的是,`state`允许指定一个默认值,但是一个良好的状态设置必须保持不同文件之间的默认值相同。如下所示: |
|
|
|
#s.update(it => 1) |
|
|
|
#code(```typ |
|
#let s1 = state("my-state", 1) |
|
s1: #context s1.get() \ |
|
#let s2 = state("my-state", 2) |
|
s1: #context s1.get(), s2: #context s2.get() \ |
|
#s2.update(it => it + 1) |
|
s1: #context s1.get(), s2: #context s2.get() |
|
```) |
|
|
|
尽管`s2`指定了状态的默认值为`2`,因为之前已经在文档中创建了该状态,默认值并不会应用。请注意:你不应该利用这个特性,该特性是Typst中的「未定义行为」。 |
上面这段代码的结果(两次s2分别是1、2)不符合 v0.14.1 实际结果(两次s2分别是2、3),也不符合文档。
If you construct multiple states with the same key but different init values, they will each use their own initial value but share updates.
https://typst.app/docs/reference/introspection/state/#constructor-init