Skip to content

backslash line continuation pre-object breaks objects #2457

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

Closed
mjbaldwin opened this issue Jul 24, 2012 · 7 comments
Closed

backslash line continuation pre-object breaks objects #2457

mjbaldwin opened this issue Jul 24, 2012 · 7 comments
Labels

Comments

@mjbaldwin
Copy link

I tend to use backslash line continuation liberally, since I've been bitten by a lot of times when CoffeeScript breaks without it, for example this breaks if any of the backslashes are missing:

switch condition
  when \
      "long first condition on one line when I want to limit line length to 80 chars", \
      "second condition on a third line", \
      "third condition, different lines for readability"
    doSomething()

But now I'm being bit by the opposite, I tried writing:

                callFunction arg1, \
                  object_key_1: data1 
                  object_key_2: data2

But it compiles, bizarrely, to:

callFunction(arg1, {
  object_key_1: data1
});

({
  object_key_2: data2
});

I can't imagine why it's splitting up the object. Removing the backslash produces the desired behavior:

                callFunction arg1, 
                  object_key_1: data1 
                  object_key_2: data2

produces

callFunction(arg1, {
  object_key_1: data1,
  object_key_2: data2
});

Now, backslashes aren't even a documented functionality of CoffeeScript, at least not that I can find on the website, despite the fact that they're sometimes required for multi-line functionality (as in the switch above). And I can't figure out for the life of me when they're required, and when they're not, except through trial-and-error.

But the fact that they break existing code when they're added in places where they should just be redundant, seems like a bug, unless there's something additional that backslashes mean that I'm missing.

Basically, shouldn't I be able to add a backslash to the end of any line (except perhaps object property lists, and of course multiline strings), without it changing the meaning of my code? So I'm filing this as a bug... Please correct any misunderstandings I may have here, though!

@michaelficarra
Copy link
Collaborator

I think you have a big misunderstanding about backslashes. Backslashes cause the following newline to be ignored. Would you expect

callFunction arg1,                   object_key_1: data1 
                  object_key_2: data2

to compile? Of course not.

@mjbaldwin
Copy link
Author

Ah, got it.

Was a misunderstanding indeed -- I thought backslashes just "forced" the next line to be interpreted as part of the same statement, not that they literally removed the newline from parsing.

Is there a way to get this backslash behavior officially documented? I'd do a whole pull-request thing with the website and add it myself, but I don't know if that's the preferred way or not for CS.

Anyways, thanks, closing the issue.

@michaelficarra
Copy link
Collaborator

Pull requests are always welcome. Whether or not we want line-continuing backslash in the documentation is a different question.

@mjbaldwin
Copy link
Author

Right, I'd be happy to add it, but only if it's wanted.

My $0.02 is that I wish the CS docs were much more comprehensive; I feel like half of what I use day-to-day in CS is undocumented and was learned from frustrating trial and error, and a lot of time could have been saved if things like backslashes were in the docs, since they're clearly a core and necessary language feature to write things that can't be achieved any other way (like the switch statement at the top of this issue).

@michaelficarra
Copy link
Collaborator

See #1808 and #1631 to start.

@jashkenas
Copy link
Owner

... for what it's worth, the reason they're not in the docs is because you're not really supposed to ever have to use them. I'd write your initial example like this:

switch
  when condition(a)
    doSomething()

... where condition is an extracted function that does what you want.

@mjbaldwin
Copy link
Author

Hi Jeremy, thanks for the suggestion.

First of all, I didn't even know you could use a switch statement without a parameter after the switch -- another undocumented feature! :)

Although I feel like hiding the switch values inside a function defeats the whole purpose of the switch statement, which is to be able to compare a single values to other sets of values in a straightforward (and, above all, visible) way.

To write a maintainable solution without using backslashes, I suppose I'd have to rewrite the whole switch as a big if statement. Oh well, thanks for the feedback!

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

No branches or pull requests

3 participants