Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

V-lang shows V panic: array index out of range error for valid indexing of array after V panic is encountered once

Tags:

arrays

vlang

There is this new programming language V-lang being created by Alex Medvednikov. I'm using V-lang version 0.1.11 currently. I can declare an array in V-lang like below :

a := [1,2,3]
// or, mut a := [1,2,3]

I tried to get the last item of this array like :

>>> a := [1,2,3]
>>> println(a[-1])
V panic: array index out of range: -1/3
>>> println(a[a.len -1])
V panic: array index out of range: -1/3

Each time, it shows :

V panic: array index out of range:

Now just after this, if I try to get the items from the array, then still it shows the same error :

>>> println(a[1])  
V panic: array index out of range: -1/3
>>> println(a.len)
V panic: array index out of range: -1/3

Where as, if we tried to get the items from the array before once we have had encountered V panic, it would have printed the same without any error, like a fresh instance in the terminal :

>>> a := [1,2,3]
>>> println(a.len)
3
>>> println(a[1])
2

Why does V-lang shows V panic for valid indexing every time after once we encounter V panic beforehand ?

like image 481
Arkistarvh Kltzuonstev Avatar asked Jun 28 '19 08:06

Arkistarvh Kltzuonstev


1 Answers

That's probably a bug in V REPL. You can file an issue here

Unlike Python, V-lang doesn't have this feature to get elements from the end of an array with negative indexes

a := [1,2,3]
a[-1] //isn't valid

The official documentation is short and precise

mut nums := [1, 2, 3]
println(nums) // "[1, 2, 3]"
println(nums[1]) // "2" 

nums << 4
println(nums) // "[1, 2, 3, 4]"


nums << [5, 6, 7]
println(nums) // "[1, 2, 3, 4, 5, 6, 7]"

mut names := ['John']
names << 'Peter' 
names << 'Sam' 
// names << 10  <-- This will not compile. `names` is an array of strings. 
println(names.len) // "3" 
println('Alex' in names) // "false" 

// We can also preallocate a certain amount of elements. 
nr_ids := 50
ids := [0 ; nr_ids] // This creates an array with 50 zeroes 

 //....
like image 176
shb Avatar answered Nov 15 '22 10:11

shb