-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mutating immutable variable #21080
Comments
Thoughts @medvednikov @spytheman ? |
This is why pointers are so fraught... Yes, |
so that's not a bug? it's a feature? |
It's not a "feature", per se... it is simply the way things work, IMO. |
It's a bug. V doesn't have mutable types (&User vs mut &User). The checker simply needs to improve to handle this. It already prohibits |
However, the code here isn't modifying Creating an immutable reference does not change the mutability of the field. |
I think it should not be possible to create a mutable reference to an immutable variable: Same as i := 0
mut ref := &i Results in:
|
V immutable variable isn't JavaScript's const i guess. Fields and variable itself shouldn't be mutable if it's not declared as mut, am i right? |
Correct. "Immutable by default." This is opposite most other languages. |
Describe the bug
Code: https://play.vlang.io/p/6ff5de297d
Reproduction Steps
create a function that returns reference from arguments, set returned value as mutable variable, mutate original variable
Expected Behavior
parser error
Current Behavior
Output:
without any issue
Possible Solution
maybe we should annotate if returned reference should be mutable:
or maybe for every value we should let it for be mutable or not, it will complicate a langauge a little but definitly will help with this error
Additional Information/Context
No response
V version
V 0.4.5 29e5124
Environment details (OS name and version, etc.)
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.
The text was updated successfully, but these errors were encountered: