V: Change array initialization syntax

Created on 1 Dec 2020  路  13Comments  路  Source: vlang/v

Currently we have this kind of syntax:

_ := [u32(10), 5, 10]

The array type is inferred to []u32
The syntax should change to a cast like initialization:

_ := []u32([10, 5, 10])

This should also work for sum types.

Compiler Type System

All 13 comments

Will the latter one not make it look like a function?

no, like a cast.
u32(0) is also not a function

Ok, it is casted to []u32() like how it is done in Go for []byte and []rune.

yep, so rather than casting the first element of the array, this new syntax would cast the array itself.

what would happen for this?

a := [1,2,3]
b := []u64(a)

@spytheman i think a is array of any_int and b is array of u64

a is an array of just ints, my question is more about what should []u64(a) do:
1) produce a checker error
2) generate code for looping over a, and producing a new array of u64 elements.

A cast usually does no looping at all, it just changes how the underlying data is treated, but I do not see how will that work for arrays (any container types really, maps will also need to be considered).

How does this behave?

a := 1
b := u64(a)

_edit:_

int a = 1;
u64 b = ((u64)(a));

_edit2:_
since this:

b := a

is a copy, a new array could be created with u64 elements.

a cast of a single element just changes how it is handled; it does not change the data itself, and is easy to do
a cast of an entire array has potentially large ramifications

What would be your suggestion?

checker error might be okay. let's see what @medvednikov says.

+1 for checker error, only array literals should be castable.

It's an interesting problem. Go solves it by not allowing to cast between arrays :)

We can do the same.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

radare picture radare  路  3Comments

taojy123 picture taojy123  路  3Comments

cjmxp picture cjmxp  路  3Comments

lobotony picture lobotony  路  3Comments

clpo13 picture clpo13  路  3Comments