a bad bug
>>> mut arr := [[0].repeat(3)].repeat(3)
>>> arr[0][0] = 3
>>> print(arr)
[[3, 0, 0], [3, 0, 0], [3, 0, 0]]
:(
but
>>> mut arr := [[0, 0, 0],[0, 0, 0]]
>>> arr[0][0] = 3
>>> print(arr)
[[3, 0, 0], [0, 0, 0]]
@sina-carbon It looks like it has same behavior with python:
>>> a = [[0]*3]*3
>>> a[0][0] = 3
>>> a
[[3, 0, 0], [3, 0, 0], [3, 0, 0]]
>>> b = [[0,0,0],[0,0,0]]
>>> b[0][0] = 3
>>> b
[[3, 0, 0], [0, 0, 0]]
@buttercrab
I think it's because the ram address is the same
Can we also using the * instead of repeat? it look so neater some.
@yuyi98
No :( ,It's a good idea, but if it's corrected
there's now much nicer syntax:
[]int{len: 3} => [0,0,0]
[][]int{len:3, default: []int{len:3}} => [[0,0,0],[0,0,0][0,0,0]]
@medvednikov
ok , thanks
but
error: expr(): bad token ]
@medvednikov
[]array_int{len:3, default: [0, 0, 0]} => [[], [], []]
[]array_int{len: 3, default: []int{len: 3}} => [[], [], []]
@medvednikov It's not working
For usage information, quit V REPL using `exit` and use `v help`
V 0.1.27 8f6d69b
Use Ctrl-C or `exit` to exit
>>> []int{len: 3}
V panic: array.get: index out of range (i == 0, a.len == 0)
0 v 0x0000000108bae76f array_get + 95
1 v 0x0000000108c04b52 v__parser__Parser_top_stmt + 786
2 v 0x0000000108c03b74 v__parser__parse_file + 2084
3 v 0x0000000108c05280 v__parser__parse_files + 256
4 v 0x0000000108c13449 v__builder__Builder_gen_c + 105
5 v 0x0000000108c13c10 v__builder__Builder_build_c + 272
6 v 0x0000000108c13fe8 v__builder__Builder_compile_c + 792
7 v 0x0000000108c187e8 v__builder__compile + 136
8 v 0x0000000108c4d9af main_v + 2959
9 v 0x0000000108c1da9e main + 78
10 v 0x0000000108bab144 start + 52
11 ??? 0x0000000000000004 0x0 + 4
@buttercrab
I think the problem is with the REPL
try in REPL:
>>> a := []int{len: 3}
@buttercrab fixed . bd2129
@sina-carbon Good It works
Yes like @buttercrab said, Python has exactly the same behavior. It took me quite a while to locate this "bug" and understood what was happening when first writing Python code using 2D array.
And that's exactly one of my dislikes of Python - Sometimes addresses fool you.
I hope this can be solved in V - I believe by writing arr[0][0] most programmers mean changing only one element rather than the whole first column. Even if they really want, they should write, e.g., arr[:, 0] = 0.
Fixed in #5357.
Most helpful comment
Yes like @buttercrab said, Python has exactly the same behavior. It took me quite a while to locate this "bug" and understood what was happening when first writing Python code using 2D array.
And that's exactly one of my dislikes of Python - Sometimes addresses fool you.
I hope this can be solved in V - I believe by writing
arr[0][0]most programmers mean changing only one element rather than the whole first column. Even if they really want, they should write, e.g.,arr[:, 0] = 0.