You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the documentation states that V references are similar to Go Pointers. When experimenting with them, I've witnessed following behavior:
mut a := 4
mut ref := &a // reference to a
mut b := 10
ref = &b // reference another variable
println(a) // as expected in any language: 4
This behaves just like you would expect pointers / references to behave. Assigning a new reference-value to ref should not affect the original variable in any way.
However, when the same type (int-reference) is a function parameter, everything changes:
fn foo(mut ref &int) {
println(typeof(ref).name) // confirm: this is a int-reference !!
mut b := 10
ref = &b // this is actually equivalent to *ref = *(&b)
println(ref) // this is expected: 10
}
mut a := 4
foo(mut a)
println(a) // 10
Another example:
As expected, this does not work because of a type mismatch:
mut a := 4
mut ref := &a
ref = 10 // error: cannot assign to `ref`: expected `&int`, not `int literal`
However, here we go again inside a function:
fn foo(mut ref &int) {
ref = 10 // auto-de-references ref and assigns the value to it
}
mut a := 4
foo(mut a)
println(a) // 10
We can see, if the reference is a function parameter, reassigning a reference will not replace the reference, but therefore:
implicitly dereference the provided reference (right hand side)
assign this value to the referenced variable (left hand side)
Suggestion:
This is inconsistent behavior when working with references. I'd suggest removing the auto-dereference when assigning to (function parameter) references.
I'd expect references (types in general) to behave the same, no matter if it is a function parameter or a local variable.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
the documentation states that V references are similar to Go Pointers. When experimenting with them, I've witnessed following behavior:
This behaves just like you would expect pointers / references to behave. Assigning a new reference-value to ref should not affect the original variable in any way.
However, when the same type (int-reference) is a function parameter, everything changes:
Another example:
As expected, this does not work because of a type mismatch:
However, here we go again inside a function:
We can see, if the reference is a function parameter, reassigning a reference will not replace the reference, but therefore:
Suggestion:
This is inconsistent behavior when working with references.
I'd suggest removing the auto-dereference when assigning to (function parameter) references.
I'd expect references (types in general) to behave the same, no matter if it is a function parameter or a local variable.
Beta Was this translation helpful? Give feedback.
All reactions