-
Notifications
You must be signed in to change notification settings - Fork 17.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: $ for array length in index #16231
Comments
I would be loath to add a new character to the language just to avoid writing |
I'm reasonably sure that we discussed this when designing slices, and decided against it, for a variety of reasons:
I am voting against this proposal. |
If you have an international keyboard, you can almost have what you want ;) |
The purpose of adding the alias is less to simplify the slice range (although I still contend that it's easier to read), and more to make one-line operations possible. |
@Shriken Being able to write the shortest possible code is not really a goal of Go. While in general shorter is better than longer, clarity and simplicity are more important. |
Given the general negative response to this proposal, I am declaring it "declined." The comments above provide sufficient rationale for this decision. |
Negative indexing is a common request from / point of contention for new Go programmers, and has been for nearly seven years.
(2015, 2013, 2011, 2009)
The main argument for is ease of access, while the main argument against is that it introduces risk of insidious bugs where an unintentionally negative index can drastically (and silently) change the properties of a slice.
I haven't seen any mention of how D does things, which is to make
$
an alias for the length of the array/slice anywhere between brackets. For example:Written in go syntax:
This alias allows for pleasantly terse and readable slicing without risk of off-by-one errors causing silent errors. It also enables idiomatic single-line grabs, eg. taking the last element in a csv row:
fmt.Println(strings.Split(row, ",")[$ - 1])
.The text was updated successfully, but these errors were encountered: