Understanding mutability #8648
Replies: 2 comments 2 replies
-
|
The struct User {
name string
mut:
is_registered bool
}The other use, on a function argument, as in fn (mut u User) register() {
u.is_registered = true
}is where you require permission from the caller the mutate the struct they've given you a reference to. Mutability is not inherently dangerous to your program, but uncontrolled mutability is. Languages differ slightly in how they deal with this (if at all): marking individual fields as mutable or not is kinda OO-style whereas reference-level controls come from Rust. V is a bit unusual in that it has both styles of mutability controls at once. |
Beta Was this translation helpful? Give feedback.
-
It means its value can be changed. If you don't declare
Immutable fields are useful to prevent direct changes to those fields, whilst allowing mutable fields to change. Or even with all fields immutable, a user can assign another struct to a mutable instance of the struct - but they can't directly change immutable fields. This can help to enforce creating only certain valid values of a struct, by requiring the user to call a function/method. To truly enforce this, we should probably make it illegal to name private fields in a struct initializer. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I find the mutability concept in V somewhat confusing.
I've read the documentation, and didn't really find answers to my questions.
Looking at the example from the documentation:
Here, the
is_registeredfield in the struct is declared mutable - it's pretty clear what that means, that the value of the field can be changed.Now, in the function declaration, the argument
mut u Useris also declared mutable. Here's where I start to get confused. What does it mean for an argument to be mutable? We've already declared that the field is mutable. Yet, the compiler demands we make the function argument mutable as well. Why?And then, when we declare the
mut Userinstance, the local variable is also declared as mutable. What does it mean for a local variable to be mutable? Again, the field was declared as mutable, but the compiler demands we make the local variable mutable as well. Why?To make one mutable field, we had to make three mutable declarations.
The only reason I can think of, is you want to be extremely explicit about which functions could enact side-effects?
But if that's the reason, it seems the keyword
mutmeans two very different things: in structs, it makes fields mutable, that's pretty clear. But in argument and variable declarations, it doesn't seem to have any direct effect on the symbol itself? It doesn't make something mutable, it's more like an assertion that says "I know that something else, like a method I call, could have effects on something mutable in this struct".I don't know, maybe that buys you some guarantees in terms of memory management or threading in the future?
But it's a bit confusing as a language concept, since
mutdoesn't appear to mean one thing.Also, what happens if you declare a mutable argument or variable, but the struct/type itself doesn't have any mutable fields? For example, a function with a
mut value intargument can't somehow change the non-local value of a variable being passed to it, can it?If this is all by design and working as intended, it really needs better documentation - like a whole section explaining mutability, what it is and when to use it, etc.
Beta Was this translation helpful? Give feedback.
All reactions