Array appending sometimes gives strange results
For example,
fn main() {
mut a := [1, 2, 3]
mut b := a
a << 4
b << 5
println(a) // => [1,2,3,5] √
println(b) // => [1,2,3,5] √
b << 6
b << 7
mut c := b
c << 8
c << 9
println(a) // => [0,0,3,5] ?
println(b) // => [0,0,3,5,6,7] ?
println(c) // => [1,2,3,5,6,7,8,9] √
}
And if I delete the first println(a) and println(b), the results will be correct.
fn main() {
mut a := [1, 2, 3]
mut b := a
a << 4
b << 5
b << 6
b << 7
mut c := b
c << 8
c << 9
println(a) // => [1,2,3,5] √
println(b) // => [1,2,3,5,6,7] √
println(c) // => [1,2,3,5,6,7,8,9] √
}
fn main() {
mut a := [1, 2, 3]
mut b := a
a << 4
b << 5
println(a) // => [1,2,3,5] √
println(b) // => [1,2,3,5] √
Why is this ok? Where did the 4 go?
As I understand it, arrays are copied by reference when using := not by value, so a and b reference the same mutable array. Two values are appended to that array, 4 and 5, but only one is shown
@dcurrie Yeah, to begin with I also understand arrays like this (and this is a typical Python style/understanding of arrays). But if so, the locations of a and b should be the same (i.e. a==b). Actually this is not true. After some experiments I infer that the mechanism of arrays in V is probably: a := [1, 2, 3] b := a // There's only one copy in memory. b points from 1 to 3, so b is [1,2,3]. a << 4 // Append 4 to a, so a is [1,2,3,4]. But b still points from 1 to 3. b << 5 // Append 5 to b, so the coming 5 will overwrite 4. Finally, we actually have [1,2,3,5] in memory. And both a and b point from the first element to the fourth, so they're both [1,2,3,5]. In summary, in this program a and b are variables recording the starting and ending position. So this style is neither pointer(or reference) copying nor value copying.
Related: https://github.com/vlang/v/issues/119
Array copies/references need a lot of work. I've used them zero times in my code (V, Volt etc).
@medvednikov I totally understand. This is a very good way to help programmers to explore and understand the mechanism of a language. Differentiating between locations and values is always a challenge for language learners. One important reason is that many languages even do not have clear description of it. I hope V would be a clear and definite language : )
@medvednikov
Can be << replaced to add or append in future?
In first glance it like as left shift.
Also in future can be added insert and delete for arrays.
One important reason is that many languages even do not have clear description of it. I hope V would be a clear and definite language : )
For sure, that's one of the main goals. It'll be made clear.
Can be << replaced to add or append in future?
<< is going to stay. It's a much shorter and cleaner alternative to array = append(array, val).
Also in future can be added insert and delete for arrays.
They are already implemented: array.insert(i, val), array.delete(i). Should make them public, so that they appear in module docs.
See #119 for the conversation on this.