v icon indicating copy to clipboard operation
v copied to clipboard

`it` is always `int` for numeric arrays - should be same type as array

Open JalonSolov opened this issue 2 years ago • 6 comments

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.

JalonSolov avatar Jul 24 '22 15:07 JalonSolov

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.

Jhynjhiruu avatar Jul 24 '22 23:07 Jhynjhiruu

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.

JalonSolov avatar Jul 25 '22 00:07 JalonSolov

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?

spytheman avatar Jul 25 '22 08:07 spytheman

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.

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.

spytheman avatar Jul 25 '22 08:07 spytheman

I agree, it should be renamed to index.

medvednikov avatar Jul 25 '22 08:07 medvednikov

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.

JalonSolov avatar Jul 25 '22 11:07 JalonSolov