Value Types in Function Parameters #20191
Unanswered
tweekzter
asked this question in
Questions and Answers
Replies: 1 comment
-
Just a small example where this could be useful:
Sure, this is not a tragedy, but in a concise and simple language that would come in handy, especially as you can do it almost anywhere else as well. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I'm having problems getting used to the way V handles function parameters.
In most languages (C, C++, Java, JavaScript, PHP, Go, Rust, ...) you can pass by value to functions and then mutate the parameter (the local copy). That's pretty much how pass by value is explained (and tested) in any tutorial. Transfering this behavior to V, it would look like this:
This of course, does not work in V.
The compiler will tell you that mutable arguments are only allowed for arrays, structs, pointers, etc. which basically means, it just works for reference types. You cannot mutate a value type function parameter (not even a struct btw).
Although it is possible to assign the value type parameter to a new variable, which you can then mutate.
So, why not immediatly let a be mutable?
The mut keyword states that a variable is mutable. It works for value types at assignments. Why not allow it for parameters?
If "mut" in the parameter declaration is meant to indicate that the parameter is a reference (that it will change the value of the calling variable), the type annotation should be sufficient (&int, &Foo, or whatever). "mut" should not indicate a reference, but just make the variable (and eventually its reference) mutable.
Beta Was this translation helpful? Give feedback.
All reactions