-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Go 2: negative slice/array indexes #33359
Comments
|
The advantage is that when writing complex algorithms, it improves the readability for some people. It seems more intuitive for |
What are the constraints on the What happens if |
The key is to think of it as a macro. |
Treating constants differently from literals seems very non-orthogonal. Treating literals differently from variables seems surprising. Since the operation in question modifies the behavior of index expressions, I would expect it to be indicated by something that looks more like a variant of an index expression than an ordinary mathematical operator, especially for something that is merely syntactic sugar. |
Here's an alternative to consider: if you want to reduce the stutter, consider the x := someDescriptiveSliceName[_-3] |
_ seems unnecessary to me. |
This isn't purely at a compiler level because you can only know the value of an
Technicalities aside, I think that the necessity of having a special case for negative integers is a bit inconsistent, however I understand it's use because of backwards compatibility and out of necessity in order to flip the integer's sign before indexing normally into an array. But the necessary evil seems like an exception that makes the feature not feel... right. |
Sorry, I hadn't seen that. I thought I read the comments but didn't see that part.
That part in the initial proposal seems to indicate that variables are allowed. Might want to reword that and explicitly state that variables are not allowed. |
Another idea: permit |
@ianlancetaylor I like that idea. It doesn't help much if the name of the slice is go/src/cmd/compile/internal/gc/align.go Line 465 in b91b3d9
becomes
@bcmills's idea seems reasonable as well:
A couple of questions that would need to be answered with either of these:
So for example, if the answer to both is "yes", then I suppose you could do something like this (not saying it's particularly good or useful):
|
I don't think that suggestion is a big enough advantage in the real world. The governing idea of my proposal is to reduce the cognitive processing involved when reading complex algorithms. When attempting to get a gist of what an algorithm does, seeing a Also note that using Python is a language that regularly uses negative indices and it helps with readability (for me, immensely). I've never seen a Python developer complain about negative indices, but I suspect they will miss the feature if it was removed.
I don't know much about the actual developer time to implement my proposal but I suspect it would be minimal to treat it as a special case. |
I disagree. A new user of Go would have much less trouble with Sure, python allows Go has always favored verbosity in order to promote clarity, and I think that |
@pjebs Actual developer time isn't a concern here. The concern is lack of orthogonality in the language. It's confusing to add a complex new concept: an expression that can be any non-negative value, or can be a negative constant, but cannot be a negative value that is not a constant. |
@cespare I think that |
@ianlancetaylor when you write |
Yes, by "immediately" I meant to describe what should happen with |
The most common case here is probably I agree with @deanveloper that |
I prefer |
It seems that the current proposal is that The next question would be: how often does this case occur? It should be possible to search some corpus of Go code for how often we see |
@ianlancetaylor I wrote a quick regex hack in python to find single-line examples of this pattern. Found 799 examples in 379 files in the The command I ran was It only finds single-line examples of this and it can have some false-positives (although in a quick manual scan of the results I only found a couple of false positives, both as commented code). It may still help us get a general idea of how common (or not) this pattern is. Here is the quick hack as a gist: https://gist.github.com/IanTayler/1db93fe558134c7b5f540ede13045725 And here are the results of running the command for |
@IanTayler Thanks for doing that. One thing I see right off the bat is that quite a few of the expressions have multiple |
@ianlancetaylor to me, that seems an argument in favor of @bcmills's
|
To me |
Agreed with the first part that says |
Well, However, I still have doubts about whether it's worth doing anything here. In cases where a slice has a long name or its length is used more than once in a piece of code, I often cache the length in a simple variable and then use that instead. This is, in effect, what Also, as @ianlancetaylor intimated earlier, generic library functions would allow many of these cases to be expressed in a simple intuitive way. The example we're stuck on at the moment could be solved with |
That's a good point about '_' meaning “ignore” rather than “infer”. I agree that |
So my vote considering the current options (negative indices, Maybe |
If something like this goes in, I'm 👍 specifically on It's clearer than (It's been mentioned a couple times but always in posts with other proposals so just 👍'ing those is ambiguous.) |
Yes, I agree with that. Although I prefer |
The examples shown in #33359 (comment) show that using For these reasons this is a likely decline. Leaving open for four weeks for final comments. -- for @golang/proposal-review |
In my point of view. This |
No change in consensus. |
I implemented it in v1.0.3 of https://github.com/rocketlaunchr/igo Go transpiler |
Currently a negative index to a slice causes a panic.
This proposal doesn't want to change that behaviour.
...except at compiler level.
The compiler, when presented with a
-x
in the slice index pretends it is equivalent tolen(s)-x
.It is essentially a macro (syntax sugar). It only applies when a human types it in code. It does not apply if you use an
int
variable of negative value.The text was updated successfully, but these errors were encountered: