v
v copied to clipboard
`it` is always `int` for numeric arrays - should be same type as array
OS: linux, "Manjaro Linux"
Processor: 32 cpus, 64bit, little endian, AMD Ryzen 9 5950X 16-Core Processor
CC version: cc (GCC) 12.1.0
getwd: /home/jamie
vmodules: /home/jamie/.vmodules
vroot: /home/jamie/git/v
vexe: /home/jamie/git/v/v
vexe mtime: 2022-07-24 15:38:29
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.0 d9fe2ed.2d7406a
Git version: git version 2.37.1
Git vroot status: weekly.2022.29-56-g2d7406a8
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 827f7452
What did you do?
b := []u8{len:16, init: it}
println(b)
What did you expect to see?
[`\0`, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, `\a`, `\b`, `\t`, `\n`, `\v`, `\f`, `\r`, 0x0e, 0x0f]
What did you see instead?
foo.v:1:25: error: expected `u8`, not `int`
1 | b := []u8{len:16, init: it}
| ~~
2 | println(b)
3 |
It works if you change the code to
b := []u8{len:16, init: u8(it)}
println(b)
but this shouldn't be necessary.
Correct me if I'm wrong, but isn't it
supposed to be the index in the array, which should always be int
? It has nothing to do with the elements.
You correct that the index in int
. However, it doesn't make sense to have the it
syntax for something that can't be used directly. It would, however, work correctly if it
were auto-cast to the correct type.
But it can not be casted automatically in the general case; consider for example []string
, or an []SomeType
:
b := []string{len:16, init: it}
println(b)
What should be the semantic of this, if there is an auto cast?
Correct me if I'm wrong, but isn't
it
supposed to be the index in the array, which should always beint
? It has nothing to do with the elements.
That is correct - it
is the index. Imho it would be much less confusing, if it was named index
instead of it
in the init:
expression syntax as well, or if the error mentioned that it is the index also.
I agree, it should be renamed to index
.
But it can not be casted automatically in the general case; consider for example
[]string
, or an[]SomeType
:b := []string{len:16, init: it} println(b)
What should be the semantic of this, if there is an auto cast?
I specifically mentioned "for numeric arrays". Not strings, structs, etc., just numeric types such as int, i64, f32, etc., etc. - in other words, things that can be auto-cast.