Open mrtowers opened 3 months ago
Thoughts @medvednikov @spytheman ?
This is why pointers are so fraught...
Yes, rere
returns an immutable pointer... to a struct with a mutable field. Modifying that field is perfectly valid.
This is why pointers are so fraught...
Yes,
rere
returns an immutable pointer... to a struct with a mutable field. Modifying that field is perfectly valid.
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 foo := mutable_ref
.
However, the code here isn't modifying User
or &User
. It is only modifying the mutable field inside User
. That's why I said it is the way things work.
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:
error: `i` is immutable, cannot have a mutable reference to it
20 |
21 | i := 0
22 | mut ref := &i
|
However, the code here isn't modifying
User
or&User
. It is only modifying the mutable field insideUser
. That's why I said it is the way things work.Creating an immutable reference does not change the mutability of the field.
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.)