I want to create a state and assign it to a variable. In addition, the state should be added to a global list.
Is there any way to write a function like project_update that does this?
// Working
#let project_list=state("list",())
#let project(name)= {
state(name,(:))
}
#let update(pState)= {
project_list.update(o=>{o.push(pState); o})
}
#let test=project("test")
#update(test)
// Not working
#let project_update(name)= {
let p=state(name,(:))
project_list.update(o=>{o.push(pState); o})
return p
}
#let test=project_update("test")
No, that’s not possible to do. It’s because the state update is a content value by itself, even if it’s invisible to the eye (it must still be inserted into the document). Creating content and returning that is not compatible with also returning another value separately. Except if you deliberately return both those things in an array - but then the caller of the function needs to take care to receive the content and insert it into the document.
You might manage a single state holding a dictionary and put your dedicated project-information into direct or nested elements.
Handling nested dictionaries is a bit stressful though. I recommend creating helper-functions to do that.
An alternative is handing around the state manually as first argument - it seems not as elegant but is far more easy to manage than the state-based approach.
I used both concepts successfully with internal libraries and recommend the latter. One thing to keep in mind when going for nested structures is, that some functions manipulate arrays/dicts in place while others don’t and there’s currently no syntactic sugar or language help for handling nested structures.
A global overall structure is a good approach, but in any case, I need a key/reference to a specific project in my text. And that is precisely the problem.