V-lang显示V紧急情况:一旦遇到V紧急情况,数组索引超出范围错误,无法对数组进行有效索引

时间:2019-06-28 08:09:29

标签: arrays vlang

Alex Medvednikov正在创建一种新的编程语言V-lang。我目前正在使用V-lang版本0.1.11。我可以在V-lang中声明一个数组,如下所示:

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

我试图获得该数组的最后一项,如:

>>> 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

每次,它显示:

  

V紧急:数组索引超出范围:

现在,在此之后,如果我尝试从数组中获取项目,那么它仍然显示相同的错误:

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

如果在遇到V panic之后,如果我们尝试从数组中获取项目,则它将打印出相同内容而没有任何错误,例如终端中的新实例:

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

为什么在我们事先遇到V panic之后,V-lang为什么每次都会显示V panic来进行有效索引?

1 个答案:

答案 0 :(得分:1)

这可能是V REPL中的错误。您可以提交issue here

与Python不同,V-lang没有此功能可从具有负索引的数组末尾获取元素

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

official documentation简短而精确

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 

 //....