Note that Function Builder, and by extension ViewBuilder
, is not part of the language (yet) so the behavior is not exactly stable. Which means that
- Any explanation here may not apply in the future, and
- What can and can’t be done may also change in the future, though chances are, you can do more, not less,
Semantically, there’s no notable a distinction between let x = ...
and x = ...
. Both are assignment statements with special operator =
, though the former also declares a new variable. So I’ll guess that they specifically allow let
variable declaration to transform in a special way, which seems reasonable (remember, each line is transform by ViewBuilder
).
Now on the SwiftUI side, I did say it before that you are not to mutate states inside the view (See the rant part at the end of Function Calls in Swiftui - #3 by Lantua). This applies to all objects, not just @State
variable (just that it’d have annoying message specifically with @State
). SwiftUI reserves its right to call body
or just part of it more, or less than you’d expect.