Why is SwiftUI's View a PAT?

Allocation performance is not the primary concern. Existential boxes can be optimized out in a lot of cases. Making the view body an associated type enables larger systemic optimizations. By raising the static structure of the view hierarchy into the type system, diffing and updating become much easier, since it doesn't normally need to do a graph diff and can do a 1:1 diff of structural properties in the fixed graph. This is also an API usability benefit for the library, because statically-typed view nodes do not need a manually-assigned id like they would in similar libraries like React, because their "id" is implied by their position in the type system. This is important for the robustness of the animation system, which needs a consistent notion of identifiers across frames to be able to correctly interpolate animatable properties.

45 Likes