Skip to content

Revisit negative array indexing #1222

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

Open
ricardbejarano opened this issue Mar 14, 2025 · 4 comments
Open

Revisit negative array indexing #1222

ricardbejarano opened this issue Mar 14, 2025 · 4 comments

Comments

@ricardbejarano
Copy link

So while this was discussed in #113 back in 2016, I wonder if we should revisit this discussion given the recent changes to array slices in #1093.

With array slicing supporting negative indexes, I find it counterintuitive not to have negative indexing in regular array lookups. Especially as we seem to approximate Python's slicing, and Python does have negative indexing.

@sparkprime you made a good argument against it here, I'm curious if you've changed your mind as hinted here.

Note: I've had this related PR in go-jsonnet open for a while.

@mstandley-tempus
Copy link

appears to be fixed in v0.21.0, see: https://github.com/google/jsonnet/releases/tag/v0.21.0

Array slices can now have negative start and/or end indexes; these are now wrapped-around (similar behaviour to Python).

@He-Pin
Copy link

He-Pin commented May 7, 2025

In jsonpath, it can have negative index too

@ricardbejarano
Copy link
Author

@mstandley-tempus no, v0.21.0 includes negative array slicing, I'm asking about negative array indexing.

$ jsonnet -version
Jsonnet commandline interpreter (Go implementation) v0.21.0

$ jsonnet -e 'std.slice([0,1,2,3], -1, null, null)'  # slicing, this is what was added in v0.21.0
[
   3
]

$ jsonnet -e '[0,1,2,3][-1]'  # indexing, this is what this issue is about
RUNTIME ERROR: Index -1 out of bounds, not within [0, 4)
	<cmdline>:1:1-14	$
	During evaluation	

$ 

@mstandley-tempus
Copy link

oh word -- i didn't read the release notes carefully enough, because i was expecting [-1] to work in 0.21.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants