Skip to content
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

can't use comma operator when indexing #1337

Closed
JayXon opened this issue Aug 6, 2024 · 6 comments
Closed

can't use comma operator when indexing #1337

JayXon opened this issue Aug 6, 2024 · 6 comments

Comments

@JayXon
Copy link

JayXon commented Aug 6, 2024

[2][1,0] is valid js, but does not parse as civet, I don't see this mentioned in https://civet.dev/comparison, so assuming this is a bug
https://civet.dev/playground?code=WzJdWzEsMF0%3D

@edemaine
Copy link
Collaborator

edemaine commented Aug 6, 2024

In general, Civet is a bit more restrictive on where it allows the comma operator, to prevent certain ambiguities. We should probably document this, though it's a bit subtle exactly where it's allowed (and is still a work-in-progress).

That said, we could support comma operators in MemberBracketContent by changing PostfixedExpression to PostfixedCommaExpression (which allows for a postfixed expression or a comma operator expression). Do you have an application where this is useful/used, or is it just a JS-compatibility/documentation issue?

I could imagine that it might be useful to define x[a,b] to mean something else. For example, I use x[[a,b]] in a bunch of code (equivalent to x[`${a},${b}`]), and x[a,b] might be reasonable shorthand for this... Leaving room for notation like this might be a reason to not allow comma operator in this context.

@STRd6
Copy link
Contributor

STRd6 commented Aug 6, 2024

Another possibility could be comma operator as an extension to ranges like:

items[0, 2, 3]
[0, 2, 3].map((index) => items[index])

@bbrk24
Copy link
Contributor

bbrk24 commented Aug 6, 2024

I believe C++ had a breaking change relatively recently that allowed multi-argument indexing, changing a[1, 2] from using the comma operator to calling a 2-argument overload of a::operator[]. You can still force the comma operator with parentheses, i.e. a[(1, 2)].

@JayXon
Copy link
Author

JayXon commented Aug 6, 2024

Do you have an application where this is useful/used, or is it just a JS-compatibility/documentation issue?

I'm only using it in code golf, so it's just an undocumented compatibility issue, especially this incompatibility doesn't bring any benefit.

I think using it as an extension to map the index is quite nice.

@Steffan153
Copy link

Another idea could be for indexing with length like Ruby's syntax, e.g. a[b,2] = a[b...b+2]

@edemaine
Copy link
Collaborator

edemaine commented Aug 6, 2024

I added to comparison.md a note about comma operator being forbidden in some contexts, including this one, and mentioning the workaround of wrapping in parentheses. If there are others you run into, we can add to the list. So I think the original issue is done.

It sounds like we have several possibilities for the meaning of a[b,c]:

  • Multidimensional matrices:
    • a[[b,c]] (poor man's matrices)
    • a[b][c] (slightly less poor matrices, and generalizes to all sorts of indexing, but questionable whether it's any more concise)
    • If TypeArrays had a specifiable dimensions, a[b*a.dim0+c] would be neat... but I think there are too many unknowns here for a language feature. Should be a library feature.
  • [a[b], a[c]] (disconnected range slice)
  • a[b...b+c] (Ruby's range slice with length)

It'd be interesting to see motivating examples for any of these, either here or in Discord.

I currently like the disconnected range slice, which could be generalized to fun things like a[0..10, -10..] (show beginning and end of string, though doesn't work great if the string is shorter than 10, and a real application might want to put some text like ... in between...).

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

No branches or pull requests

5 participants