Comments (6)
Another possibility could be comma operator as an extension to ranges like:
items[0, 2, 3]
[0, 2, 3].map((index) => items[index])
from civet.
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.
from civet.
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)]
.
from civet.
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.
from civet.
Another idea could be for indexing with length like Ruby's syntax, e.g. a[b,2]
= a[b...b+2]
from civet.
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
TypeArray
s 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...).
from civet.
Related Issues (20)
- Optional `@?x` fails to parse HOT 5
- `<?` without whitespace compiles without the whitespace
- Slice operator doesn't handle operators with lower precedence than `+` properly
- CLI execute flag
- ternary requires space HOT 4
- `%/=` and `รท=` does not work
- Fat pipe (`||>`) does not play nice with typescript-eslint v8 HOT 2
- expressions in the middle of a comparison chain are evaluated twice HOT 1
- `โ=` does not work
- `!=` not supported in chained comparison
- multi assignment expressions are evaluated twice HOT 1
- CLI doesn't work globally
- sometimes space is required for lambda
- Object union type is misparsed HOT 2
- Postfix conditional with nested `(and)`/`(or)` conditions seems like it ought to work HOT 2
- Cannot read properties of undefined (reading '3')
- Debugging in VS Code HOT 4
- Non-identifier operators HOT 2
- Postfix `if`/`unless` in types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from civet.