Hey Typst community ![]()
I’ve been experimenting with Typst and ran into a design question I’d love your insight on. Has anyone successfully implemented a caching mechanism using state?
As you probably know, Typst doesn’t allow mutation of variables outside of functions, which makes traditional caching tricky. I was wondering if it’s possible to use state to cache a dictionary of loaded data.
Here’s the idea:
- I start with an empty state dictionary.
- I call a function
foo(a)which checks ifaexists in the state dictionary. - If it doesn’t, the function loads data from
a.toml, and updates the state so that the dictionary now containsa: (some dict). - If
foo(a)is called again, it findsain the state and uses the cached data. - Later, if
foo(b)is called, it checks the state, sees thatbisn’t there, loadsb.toml, and updates the state accordingly.
I tried playing around with state, but I’m struggling to understand how to update it properly. The documentation mentions using eval, which I assume is because accessing a state value gives you its content, not a reference—so using .insert() directly doesn’t work.
Is this kind of caching pattern even feasible in Typst? Or is there a better way to approach this?
Thanks in advance for any thoughts or examples!
