Group Group Group Group Group Group Group Group Group

"An instance of a structure is an immutable value"?

First of all, thank you for the great book, Swift Apprentice.

In Swift Apprentice 5th edition, I’ve read the following sentences:

“In Swift, an instance of a structure is an immutable value whereas an instance of a class is a mutable object.” (p. 311)

“As you’ve read before, instances of classes are mutable objects whereas instances of structures are immutable values.” (p. 320)

"If you had tried this with a struct, you’d have wound up with a compiler error, because structures are immutable. Remember, when you change the value of a struct, instead of modifying the value, you’re making a new value. The keyword mutating marks methods that replace the current value with a new one. With classes, this keyword is not used because the instance itself is mutable.” p. 322

I can not entirely understand the concept that “structs are immutable values”. When we declare a struct as a variable and when we declare the properties of the struct as variables, are we not able to mutate that instance of the struct? Then, how come that struct instance is an “immutable value”?

struct Car {
    var color: String
}

var myCar = Car(color: "Red")

myCar.color // Red
myCar.color = "Green"
myCar.color // Green

In the code above, is not myCar instance of the Car struct mutable?

And when we declare a mutating method in the struct, we can again change the Struct instance. Isn’t it mutable in that case?

“Remember, when you change the value of a struct, instead of modifying the value, you’re making a new value.”

As far as I can understand, “value” means the Struct’s instance here. When we create a Struct instance, we create a “value” (not a reference). My guess is, when we change that instance, we create another “instance” or “value” behind the scenes, am I right? Can you please convey this sentence in a more easy-to-understand way for beginners?

I hope you can clarify these for me, and maybe in the further editions of the book if you think some clarification might be useful for some readers.

Thank you!

@ertugrul Thanks very much for your question! (And cool name by the way!)

A struct is a value type. What that means is, if it is used in a particular scope like a function, any changes made to that instance are only affected on THAT particular instance. If you then use an instance of that struct in ANOTHER location within the app, the instance in the NEW location is NOT the same one as you used in the original location, but a copy.

For example:

You have a text document on your computer. You then proceed to make changes to this document. You then decide to send this document to a friend. If this document is a struct, then your friend gets a copy of the ORIGINAL document. This means your friend will not see the changes you made, and nor will you see the changes they make.

If your document is a class, then think of it as a Google doc. If you decide to give access to your friend, then this means any changes you make, your friend will see it, and any changes your friend makes, you will see it as well. :slight_smile:

I hope this makes sense!

All the best.