-
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
Implicit switch if function has single parameter and consists of when clauses #2490
Comments
Cool. I'd rather adopt the LiveScript behaviour of switching on truthiness than switch on the first param. It can be applied to all functions, not just 1-parameter functions. |
While this is neat, at least that example would be better written as @actions.on 'click', (action) ->
if action in ['undo', 'redo', 'compose']
@[action]() I +0 on this. I think most use cases would be like the above, but it still has some utility. |
This strikes me as awfully narrow. Additionally, I don't find the -1 |
I'd consider this a non-issue, as we would probably switch on @actions.on 'click', ->
when "undo" ... could compile to this.actions.on("click", function () {
switch (arguments[0]) {
case "undo": ...
}
});
I see no need for this to apply to functions with just one parameter. No reason we couldn't have calculate = (op, operand1, operand2) ->
when "plus" then operand1 + operand2
when "minus" then operand1 - operand2 Which of course is highly contrived, but you can see the point. |
Ditto. That sounds like a neat way to go. |
Merging in a patch for @satyr's "guard-style if" form. Should address this nice and clearly:
Or, more compactly:
|
@jashkenas: I find that really weird. So now blocks can follow any expression? Everywhere else a block starts, something on that line at least indicates what it's doing. And I don't see what this new syntax gets us over condition-less switches. |
Nope. Just in this particular case of the expression being the condition. It's like:
... but with the
Clarity. They're not switches, they're But thanks for the feedback -- anyone else have stylistic objections? It's certainly not too late to kill these. |
Woot, quick adoption. My only gripe for this syntax is it doesn't look good on its own due to the lone |
This is great. It is the same as Clojure's |
thumbsup <3 maybe you could come up with something like ?: cond and why stop there ... at times I feel the need for a "finally" in a switch ... you know, that's always when you write this switch to see ... ahh, that's basically the same functionality in all the conditions, I should sum that up in a centralized foo. OK, but then, it's just that tick different you really are spending time on pure elegance for ...?? what exactly, yes, your ego or self-justification of poor looking code... fill in good reasons... Also ... you might want to provide an injector for the implicit if ... if raw_switchvar the scenario is similar to the previous with the finally idea... but slightly different. okay!!! cheers, good job! |
What's wrong with this?
Or this?
The newline just kinda bugs me. Doesn't seem like the cleanest option available. I like simply using That said, whatever format you roll with – this is certainly a welcome feature :) |
I just don't get why we'd want to drop the |
Because we want the language to work out. I can imagine an alternate past where we never used
... but that's not the precedent, I'm afraid. |
But what does it buy us over regular |
Yeah not a fan :( |
One less
becomes:
What does your preferred (LiveScript's) "implicit
↓
Moreover, we now could ditch topicless |
I'd love to do that. Do you think it would break a lot of folks, or do you think topicless switches are rare beasts that haven't been used by many? |
I'd say it's a common use, but github search is basically unusable at that point. No way to search topicless switch and at that point, the 8 first pages are 2 snippets repeating over and over. |
I've used topicless switches a bit to accieve things similar to the discussion above but would be happy to replace them with a more compact syntax. |
I'm not too convinced about the extra indentation. Maybe something like this:
|
Seems a bit strange that you can have lines (the cases) that don't appear to do anything special unless you have the context of the previous lines (the if). This issue is avoided with switch/when as each line has the necessary context. It makes me wonder why people couldn't just do the following if they wanted such abilities.
But either way, I'm okay with it, it doesn't seem like it will cause regressions, just doubt I would ever this over the more explicit/readable options prior to this. |
The
|
Not a fan of the multi-condition
That's a fair point. But i personally see the |
Wouldn't work due to ambiguities as in:
The extra indent is beneficial when used as guards, assigned, or called.
read better than:
|
Sure, at the expense of a little less readability due to more parsing context. Also, an additional syntactic feature with an additional indentation level, as opposed to this proposal, which gives the option for one less indentation level.
One less level of indentation when it's unnecessary. Putting the
Moot point. When they have the same semantics, we can compile them to the same thing. It's not even difficult to detect those cases. |
One issue I have with this addition is that
I find the |
@davidchambers: That example with everything unimportant masked off as dots is very compelling. Brilliant idea. |
Shall we adopt this same treatment for |
Alright. I had a working branch going with the same treatment for
But this is pretty terrible. It would be easy to forget entirely what you're looking at if you jump in right in the middle:
|
switch a
in b
foo bar
[...a]
do baz ? ( @jashkenas and @satyr ) |
Woot, quick rejection. Nice troll. |
What is really nice about the proposed syntax is that it is easy for the eye to spot the conditions because they are all on the same line. I like it. |
@jashkenas: Has the original proposal been rejected as well? I'm still for adding that or an equivalent. |
Yes, I think so. We shouldn't be encouraging folks to write topic-less switches. |
Hi, I'm sorry if this has been proposed and/or rejected before.
In Nemerle and maybe some other languages, you can omit
switch
statement if function has just one parameter and contains no statements other thanwhen
clauses.In this case, the compiler will assume you want to switch on the first parameter.
Could be re-written as this:
with the compiler assuming you're
switch
ing onaction
.Can this kind of syntax sugar make its way into CoffeeScript?
The text was updated successfully, but these errors were encountered: