-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Independent splats in array pattern matching #870
Comments
Not as nice, but you can write:
|
I don't see why not. Technically, it doesn't actually make sense to introduce a |
You'd want a placeholder syntax that takes one space as well.
In SpiderMonkey, empty entries serve well.
|
@satyr: good idea. I'd be in favor of the empty entries. The first syntax you mentioned would work fine right now if you didn't care about the value of the |
Of course though,
works just as well. |
Ah this comes back to life. I need to dig up the old discussion we had on the topic. EDIT: Start with #86, move on to #277. EDIT 2: Thought I'd share http://githubissues.heroku.com/#jashkenas/coffee-script/ is great for searching. |
Yes, I like weepy's proposal at issue 277 of using
|
In fact, it'd be cool to see
I understand the "named values contribute to more self-documenting code" argument Jeremy gave in issue 277, but I feel like that benefit is usually outweighed by the clarity that comes with avoiding the declaration of a variable you never use. For instance,
makes it much more clear that you only care about the first and last values of the array than any possible name for the middle value would. And, of course, there are the side benefits of brevity and slightly more efficient JavaScript output. Would someone like to submit a patch? |
What would |
A syntax error. It's not necessary for the pattern-matching syntax to be completely parallel to the array creation syntax. |
I don't think we need this for 1.0 ... Even if you're not going to use a variable that's serving as a placeholder, at least you can name it something descriptive. The only place that placeholder syntax would be used is in pattern matches... So, closing as a wontfix. |
Coco now supports both of those without needing this proposal, in the form of
|
satyr: want to link to your patch? |
Could we reopen this issue? It was closed with the comment "I don't think we need this for 1.0"; now that 1.0 has successfully been released, I believe it merits further discussion. Specifically, I'd like to propose allowing
and
Here's a common use case for the latter: I run a regex, and I only want the group matches (or perhaps a subset of group matches), not the full match. For instance, let's say that I have a
The closest I can come with the current syntax is to either 1) put in an unnecessary variable name instead of
The Similarly, if I have a function call
is clearer than any existing syntax, and would generate more efficient code than any existing one-liner. |
I am in support of the |
What don't you like about the single-value skipping syntax? Is it just that the The regex use case is a strong one. I frequently run matches just for the groups; adding |
@TrevorBurnham: It's not the syntax. I think a single value can always just be given a name. Skipping more than one value without naming it is more useful, though, because those skipped values may have nothing in common and thus no valid identifier. I like the hanging commas, but I remember jashkenas thought that it wasn't very readable, which I can pretty much agree with. |
I suggest |
I'd suggest using some other symbol than Note that
Just remove the extra |
@sethaurus: I like that idea a lot. |
I'd be OK with the syntax
Note, however, that the ideal compilation would be |
@TrevorBurnham: That's coffee. Go ahead and try it out. |
Cool. So it's just a matter of getting rid of the extra symbol and using Oh, and as to splats, is
would compile to something like
|
It's more a case of allowing |
I think I'd probably prefer using the |
Yeah, they both work. It kinda makes me uneasy because it does look like we are attempting to assign a value to |
I still think
then I'm wondering why there's an attempted assignment to
then I'm wondering if there's some kind of fancy nested pattern-matching going on. Of course, Maybe
After all, it seems a little odd to allow |
Consider my one vote for using a period instead of a ? or null. A splat ... symbol does a great job indicating a bunch of "unknown" things and a single period indicates one "unknown" thing. The period is visually very small which makes it somewhat like the use of nothing. So I recommend
We already use periods for this function so why not stay with the period for the particular case of one item. |
The destructuring assignment syntax is already one of the more punctuation-heavy parts of the language ( |
|
I don't think that having a value-skipping syntax is a good idea, unless implemented consistently across the language ... ie, in function signatures, and arrays as well as destructuring assignment, and if it can be used in destructuring assignment, it should be use-able in regular assignment as well. So, let's stick to the destructuring syntax we already have (as of 4ce374b):
Into:
... an empty array or object will do. Personally, I'm going to continue to name the variables. |
Thanks for the revert. |
This all seems to be sorted out now on master.
Produces...
|
Let's say that I want to get the first and last values of an array of arbitrary length, in a nice one-line statement. I could write
But this is less than ideal from both a code efficiency standpoint and a readability standpoint, since I never use
middle
. What I'd prefer to write is simplyDo others agree that this should be allowed?
The text was updated successfully, but these errors were encountered: