【问题标题】:V-lang shows V panic: array index out of range error for valid indexing of array after V panic is encountered onceV-lang 显示 V panic: array index out of range error for valid indexing of array after V panic 一次
【发布时间】:2019-11-10 04:38:38
【问题描述】:

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 panic 以进行有效索引?

【问题讨论】:

    标签: arrays vlang


    【解决方案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 
    
     //....
    

    【讨论】:

      猜你喜欢
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 2021-01-20
      • 2021-12-04
      • 2018-02-08
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多