I also feel like I need to clarify one point that I didn’t make clear before: when I say Stack, I do NOT mean array. With an actual stack it is possible to carry forward type information or to erase it as desired.
Let me give a concrete example where I use this to propagate the type info from the top item of the stack (it should be possible to do the same thing with the entire stack of type info with a related design).
I have a drawing framework that has the concept of a “DrawContext”. This is a pair of protocols that provide access to a CGContext as well as a number of values used in things like animation. The stack nodes are implemented as a bunch of types which adhere to the protocols, and which know how to wrap another node.
There are a series of functions which make the wrapping process much cleaner, and they all return the actual type created as opposed to type erasing to the protocol. In some cases, those functions (as well as initializers) are overloaded so they can take advantage of that type information (e.g. concatenation of transforms into a single layer)
If I want a type erased version, I simply store in a variable with the protocol type. The end result is that I have an efficient functional way to represent a drawing environment (including transforms, clipping, etc...).
It should be possible to capture the whole stack’s type info by making nodes where the type is dependent on what they are wrapping. It should also allow partial type erasure, etc...
The end result should be powerful enough for whatever SwiftUI needs, but that complexity can disappear for simple use cases like HTML Builder.
Does that make more sense?
(I sometimes forget that others don’t have access to the context of my ideas)