Nevermind for the first idea.. this is even more complex than I thought.
First it requires generalized interfaces. I don't see it being a major problem in the long run.
But I thought I could work around not having value scoped type declarations but turns out it is not the case after all.
Realized that it requires dependent types. (to create new types from some value, in my case here, the specific document should have its specific Element constructor types)
Don't see that happening any time soon, emphasis on soon, unfortunately.
So I guess, I'd like to be able to hack around this by maintaining my own function-value dependencies by hand but that still requires to be able to have a stable id for function values.
Essentially, doing dependent types manually.
Should be much easier I think. Just requires a stable id/ reference for function values if I'm not mistaken?