|
|
|
|
|
by jumhyn
2087 days ago
|
|
From what I understand, SwiftUI uses this so that each "snapshot" of the View tree generated by the app can be intelligently diffed against future snapshots. This helps make rendering more efficient, as well as with things like animations. Consider a simple view like: var body: some View {
if someStateBool {
Text("True")
} else {
Text("False")
}
Suppose that initially, `someStateBool ` evaluates to `true`, and then is updated to `false` (triggering an update. If the control flow were "flattened" (i.e., without `buildEither`), the two snapshots would look like: 1. Text("True")
2. Text("False")
The algorithm wouldn't be able to tell the difference between "same view with different string" and "different view entirely". With `buildEither`, the snapshots end up looking more like: 1. ConditionalContent(first: Text("True"))
2. ConditionalContent(second: Text("False"))
Now, the update algorithm can determine that the entire view was swapped out for another on the update, rather than just changing the text. |
|
In order to give the return value a type, the if statement has to be encoded in the type system itself.