by aatd86 · 3y ago · 0 comments
You can't actually reorder these statements and expect the same UI tree.
Usually, declarative is built upon imperative and simply means that implementation details have been abstracted enough to the point that we can build the program by describing its structure and it will build itself. As opposed as defining the build steps (they are predefined if you will).
Defining insertion would be in the imperative portion of the code right?
Since there, source order matters, you would have to change the subroutine that triggers insertion by changing the direction?
When describing the tree structure however, listing the nodes (html nodes for instance) has to be in order, no?
Creating those nodes can indeed be reordered provided there is no interdependence.
the same way
var a, b; c =a + b;
can be rewritten:
var b, a; c =a + b;
Asking because it seems to me that commutativity is a hard sell here. Might be mistaken about what you mean.
It doesn't matter to me how this HTML is turned into a DOM. You could go through the HTML line by line and create the nodes. Or, if you'd think it could be faster, you might insert the children in reverse order at the correct position (e.g. start inserting the last element, then before that insert the second-to-last, and so on). All that matters is that the DOM in end behaves exactly like the one I specified.
In the end, it's the same as with SQL. It doesn't matter what exact approach you take to get me what I want, as long as the end result is what I specified.
For html, the renderer still depends on the structure of the tree.
Whether it starts from the bottom of the top doesn't matter. Because the renderer is an imperative function.
But the declarative part is the tree itself and these declarative statements are not commutative is what I mean.
You cannot insert to a node that doesn't exist.
Yeah, so it does not matter in which order you render them, as long as they are inserted/shown in the right order in the end.
You wouldn't be able to modify something that has not be rendered yet for instance.
Render itself is declarative perhaps, but that's the meta level.
Render is imperative in terms of rendering a declarative tree.
What does rendered mean? In the browser window? Why not, you can certainly modify that. If you mean not rendered in the HTML then we are now talking about two different things, because my answer was meant for when there is an existing HTMl tree already.
But that's a red herring.
The point in that the sequence, render-modify cannot be turned into modify-render. This is not commutative.
Now, render(a, b, c) where a, b, c are html nodes is a slightly different story because we consider only one function.
Here, your claim is that since it is basically equivalent to render(a) + render(b) + render(c) then it's commutable.
Well, it depends at which granularity one looks at it (subtrees...). And as the original comment mentions, in the general case, it's only true if there is no side-effects.