# Conceptual questions about Struct's immutability and property changes from within the struct and from the instance

**URL:** <https://forums.kodeco.com/t/conceptual-questions-about-structs-immutability-and-property-changes-from-within-the-struct-and-from-the-instance/164648>\
**Category:** Swift\
**Created:** [February 15, 2022, 10:18am UTC](https://forums.kodeco.com/t/conceptual-questions-about-structs-immutability-and-property-changes-from-within-the-struct-and-from-the-instance/164648 "2022-02-15T10:18:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![logi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/logi/32/102075_2.png) [@logi](https://forums.kodeco.com/u/logi)\
**Post date:** [February 15, 2022, 10:18am UTC](https://forums.kodeco.com/t/conceptual-questions-about-structs-immutability-and-property-changes-from-within-the-struct-and-from-the-instance/164648/1 "2022-02-15T10:18:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [February 15, 2022, 11:45am UTC](https://forums.kodeco.com/t/conceptual-questions-about-structs-immutability-and-property-changes-from-within-the-struct-and-from-the-instance/164648/2 "2022-02-15T11:45:32Z")

</div>

@logi You can change the value of the `a` property in this case because the `testInstance` object is defined as a variable.

Declare the `testInstance` object as a constant instead in order to make it immutable like this:

```auto
let testInstance = Test(a: 10)
testInstance.a = 1

```

The compiler flags the following error for the above code:

```
Cannot assign to property: `testInstance` is a `let` constant.

```

You can’t change the value of the `a` property this time since the `testInstance` object is immutable.

The same thing happens for the `changeA()` method in this case as follows:

```auto
func changeA() {
  a = 10
}

```

The compiler flags the following error for the above code:

```
 Cannot assign to property: `self` is immutable.

```

You can’t change the value of the `a` property this time because the `self` object is defined as a constant inside the `changeA()` method by default.

The compiler **always** adds the `self` object to the `changeA()` method under the hood like this:

```auto
func changeA() {
  self.a = 10
}

```

Use the `mutating` keyword in order to declare the `self` object as a variable inside the `changeA()` method instead as follows:

```auto
mutating func changeA() {
  a = 10
}

```

The `self` object is mutable inside the `changeA()` method in this case.

The compiler still adds the `self` object to the `changeA()` method under the hood like this:

```auto
mutating func changeA() {
  self.a = 10
}

```

The `changeA()` method behaves exactly like the `a` property this time as follows:

```auto
let testInstance = Test(a: 10)
testInstance.changeA()

```

The compiler flags the following error for the above code:

```
Cannot use mutating member on immutable value: `testInstance` is a let constant.

```

You can’t call the `changeA()` method in this case since the `testInstance` object is immutable.

Define the `testInstance` object as a variable instead in order to make it mutable like this:

```auto
var testInstance = Test(a: 10)
testInstance.changeA()

```

You can call the `changeA()` method this time because the `testInstance` object is mutable.

Please let me know if you have any more questions or other issues about the whole thing when you get a chance. Thank you!

---

<div class="post-metadata">

**Author:** ![logi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/logi/32/102075_2.png) [@logi](https://forums.kodeco.com/u/logi)\
**Post date:** [February 15, 2022, 1:35pm UTC](https://forums.kodeco.com/t/conceptual-questions-about-structs-immutability-and-property-changes-from-within-the-struct-and-from-the-instance/164648/3 "2022-02-15T13:35:54Z")

</div>

Thank you very much for the detailed explanation, it is much clearer now

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [August 1, 2022, 2:18am UTC](https://forums.kodeco.com/t/conceptual-questions-about-structs-immutability-and-property-changes-from-within-the-struct-and-from-the-instance/164648/4 "2022-08-01T02:18:32Z")

</div>

Closed on August 1, 2022, 2:18am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
