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]
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Why does V-lang shows V panic for valid indexing every time after once we encounter V panic beforehand ?
这可能是 V REPL 中的一个错误。您可以在此处提出问题
与 Python 不同,V-lang 不具备从具有负索引的数组末尾获取元素的功能
a := [1,2,3]
a[-1] //isn't valid
Run Code Online (Sandbox Code Playgroud)
官方文档简短而准确
Run Code Online (Sandbox Code Playgroud)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 //....
| 归档时间: |
|
| 查看次数: |
253 次 |
| 最近记录: |