struct Test {
var a: Int
func changeA() {
a = 10
}
}
var testInstance = Test(a: 10)
testInstance.a = 1
1.- I understand, Structs are immutable, nonetheless the Swift compiler allows me to change Property “a” to 1, after it was already initialized to 10, were Structs immutable? why this is allowed?
2.- If the Swift compiler allows to change property “a” from the instance, why it doesn’t allow to have the method “changeA” to modify “a” from within the Struct without adding the “mutating” modifier? It seems it is allowed to modify the property from the instance, but not from a method within the Struct, why?
3.- These are more conceptual questions, I fully understand that I can add “mutating” in front of the method and it will compile, but why I have to do that, if by default I am allowed to modify the property from outside the method after initialization, why the extra level of rigor with “mutating” to modify a property, that it is already declared as “var”?
I am very much interested in the theory and concepts behing Struct’s immutability, thank you very much for your help.