-
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: spec: accept x[len-1] as x[len(x)-1] #20176
Comments
len is a predeclared identifier not a keyword so this would add a keyword to the language. This is a currently valid, albeit silly, program that would be made invalid by this proposal: https://play.golang.org/p/jtQ0U9pQ1H |
Good point @jimmyfrasche - will label the issue appropriately. |
As for the proposed change itself, I think it trades less verbosity for language complexity. Like Jimmy pointed out, you're adding a new keyword. And unlike the old In the scope of Go2, I feel that verbosity like |
It's currently not valid to use builtin functions outside of a call expression. This would simply be a matter of changing it so |
@jimmyfrasche It wouldn't change the behavior of the program you wrote, because as @mdempsky pointed out,
I'll update the proposal |
@posener I disagree with changing the meaning of any existing programs. If |
@mdempsky totally agree 👍 |
I don't think this is going to happen. But if it were, a few other things to consider:
|
Allowing negative indices would cover most of the usages of [len(s)-N]. And since negative indices are pretty common, users wouldn't really be confused by them. @josharian |
Making negative indices have special meaning is not going to happen. It's been proposed and rejected a number of times in the past. The rejection reason is that it's too easy to screw up your math and underflow 0 and suddenly get crazy opposite behavior instead of bounds checks runtime errors. |
Not that I'm pushing for it, but to be clear, the proposal above was negative constant (or even literal) indices, which I believe mitigates that concern. |
For the record: Lua has a |
The refinement of allowing negative constants only is interesting and perhaps worth considering. It does add a particularly special and detailed wrinkle, though. There's nothing similar that I can think of that exists already in the language. And because of that, the exact definition will take lots of discussion and bikeshedding. Literal? Constant? Constant expression? What about cap? etc. etc. I'm not sure it's a good fit, but it is a clever suggestion. |
I think a better proposal would be to accept |
As explained above by @bradfitz, that's not a good suggestion. |
Sorry, missed that. |
There is no strong support for this specific proposal, and it requires either adding a new keyword or making some very special treatment of the predeclared identifier |
When applying the index or slice operations on a string/array/slice allow the keyword
len
inside the brackets. In that caselen
will meanlen(a)
wherea
is the referred string/array/slice variable before the brackets.For example, if s is of string/array/slice type:
s[len-1]
will be exactly the same ass[len(s)-1]
s[len-5:len-2]
will be exactly the same ass[len(s)-5:len(s)-2]
Pros:
Cons:
len
as variable name.Related issues:
The text was updated successfully, but these errors were encountered: