I've conducted a preliminary search within the Swift Forums and couldn't find a similar topic, but please close this if it's already been discussed.
When using SwiftUI, especially for beginners who are not familiar with the SwiftUI rendering cycle, it's common to write code in
onReceive(_:) methods that monitors updates to a single
@State, and then changes another
@State upon update. This kind of process changes the state again during SwiftUI's View re-rendering, which can cause undefined behavior and if my memory is correct, this implementation will cause a runtime error in future SwiftUI releases.
What if we created a special function attribute like
nonmutating? For instance, we could make an attribute named
nondrawing (the name is tentative), such that within any method this attribute is attached to, calling changes to
@State would be disallowed. Moreover, only methods with the same
nondrawing attribute could be called when calling other methods; this would prevent code that changes
onReceive(_:) processing at build time.
Or we can make the opposite attribute named
drawing just like
mutating, and only methods with this attribute can modify
@State properties, and are not allowed to be called in
onReceive(_:) process, this should achieve the same goal.
What do you think of this proposal?