Skip to content
Discussion options

You must be logged in to vote

I know structs are value types and stored on the stack

On the stack isn't important here. The important bit is that the actual value of the struct is saved into the field itself. This is why I said "but you are reassigning the field" - the act of mutation would modify the field.
Since you disallowed that (since you added readonly to the field, the only way for the compiler to enforce this with a mutable struct is to take the defensive copy.

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@leandromoh
Comment options

@Clockwork-Muse
Comment options

Answer selected by leandromoh
@leandromoh
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants