Simplify and unify array declaration syntax #17514
Replies: 1 comment
-
Personally, think these kind of changes are very problematic and destroys ease of use. Being verbose or injecting more verbosity, is why many languages are viewed negatively. There is a middle ground, where you don't want to become too cryptic and symbolic, while at the same time want to stay away from verbosity. Most people don't enjoy extra typing. Arguably, the minimum amount of typing for readability, is preferable. Of course, such is subject to opinion, so keeping a balance that the majority agrees with is not easy. I think Vlang has kind of managed to achieve a very good balance so far, debatably more so than many other languages. Anyway, a lot of care should be taken to keep such balance, and as there will be lots of pulling from different directions. It's not a good situation to subject users to constant random changes, as that can get old fast, with the next breaking or perceived as annoying change constantly around the corner. Whenever significant changes are to be made, particularly those facing users, think there should be really strong justifications. For instance,
Doesn't seem like that is truly a problem. The difference between
Users can choose to type out the array initialization, to specify certain things, without eliminating the established and easy ways of declaring fixed arrays when they don't want to do extra typing. Not seeing the advantage to the user, where a convenience and choice is taken away. |
Beta Was this translation helpful? Give feedback.
-
Ok, so... fixed arrays vs dynamic arrays. We have 2 different ways of declaring fixed arrays now -
[1, 2, 3]!
or[3]int
then store values. Dynamic arrays are[1, 2, 3]
or[]int{len:3, init: it+1}
or whatever other attributes you want to give.Not too long ago, dynamic arrays gained 2 attributes:
noshrink
,nogrow
- which together is essentially the same asfixed
I'd like to propose a few changes, to simplify things a bit... making the syntax a bit more regular, as well as getting rid of the ! at the end to mean fixed size array.
Proposal is thus: Always use what looks like the current dynamic array syntax, but add more attributes to specify exactly what type of array you want. Also, allow the attributes to be specified on the
[1, 2, 3]
style.Some examples:
[1, 2, 3]
- dynamic array of ints with 3 values. Short form of[]int{len: 3, init: it+1}
[1, 2, 3]{len:5}
- dynamic array of 5 ints, with the first 3 specified, and the other 2 set to 0.[1, 2, 3]{fixed: true}
- fixed array of 3 ints, on the heap. Something we can't get now.[1, 2, 3]{fixed: true, stack: true}
- fixed array of 3 ints on the stack instead of the heap. Same as[1, 2, 3]!
now.[]int{len: 256, init: it, fixed: true}
- fixed array of 256 ints on the heap, with values initialized from 0 -> 255.There wouldn't be any reason now to all len, cap, and init on fixed arrays, etc.
The only change some may gripe about is no longer being able to just do
[3]int{}
, or[1, 2, 3]!
.This would require several changes besides the syntax change, of course, as more routines would need to check the extra attributes on an array. It may also allow all current dynamic array methods to work on fixed arrays (with the caveat that the size can't be changed).
I'm thinking the "win" of cutting the number of ways to declare an array in half (from the current 4 down to 2) would provide a smoother syntax... less cognitive load...
"Wait, how do I make it fixed instead of dynamic?"
"How can I get a fixed array on the heap?"
"What if I don't want to initialize all the values of a fixed array? I just an array to fill in later, but I do want the first 8 elements set."
Beta Was this translation helpful? Give feedback.
All reactions