-
Notifications
You must be signed in to change notification settings - Fork 1
Array literals
Blokyk edited this page Oct 31, 2022
·
1 revision
What should the syntax for array literals be ?
Right now, it seems like this might be our best bet:
// Note: i use them in declarations here, but they're just values/expressions to be clear
let arr = int[48, 987, -1]; // length: 3, elements: 48, 987, -1
let zeroLengthArr = int[]; // length: 0
let emptyArr = int[,,,]; // length: 4, elements: 0, 0, 0, 0 (i.e. default(int))
let partialArr = int[, 9,]; // length: 3, elements: 0, 9, 0
let filledArr = int[5; 30]; // length: 30, elements: 5, 5, 5, 5, ...
let longArr = int[; 2048]; // length: 2048, elements: 0, 0, 0, 0, ...
Basically, type name in the front, with either a list of elements afterwards, or an element and a length separated by a semicolon. In both cases, an empty element expression means default(T)
(so parsing might be similar to for-loop headers.)
Obvious advantage of this approach is that it's clearly similar to how we initialize classic data types like structs and ADU. It's also a very elegant solution to "how do we get AND give type info from/to the user about an array literal," being relatively un-intrusive and pretty familiar to those used to C-like languages.
There's a couple problems however:
- Does that mean we can't get multi-dimensional indexing ? ;-;
- How do you create
multi-dimensionaljagged arrays ?