moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

Line continuation character

Open tophf opened this issue 11 years ago • 8 comments

Desperately waiting for line continuation character... So that

some_self_descriptive_variable = if something.long == something_else.even_more_long and here.we_have_another_long[variable]
                                     1
                                 else
                                     2

would become

some_self_descriptive_variable = \\
    if something.long == something_else.even_more_long \\
    and here.we_have_another_long[variable]
        1
    else
        2

p.s. \ is a just an example, not necessarily the best one.

tophf avatar Dec 04 '13 16:12 tophf

I have needed this. :+1:

One thing python has that is great is that line continuations are implicit inside parenthesis. So instead of doing something like:

if super_long_condition_omg == wow_that_is_a_huge.variable.name and \
   omg_another_bizarre.variable not in huge_function_call(super_very_long_arg1=superlongvalue, \
                                                                                    super_very_long_arg1=superlongvalue2):
    pass

you have

if (super_long_condition_omg == wow_that_is_a_huge.variable.name and
    omg_another_bizarre.variable not in 
        huge_function_call(super_very_long_arg1=superlongvalue,
                           super_very_long_arg1=superlongvalue2):
    pass

etandel avatar Dec 04 '13 19:12 etandel

I don't think inside parenthesis would be too difficult. I think I was going to first make it so you could add line breaks after binary operators.

leafo avatar Dec 04 '13 19:12 leafo

Table comprehension expressions could also benefit from this sometimes.

tophf avatar Dec 04 '13 20:12 tophf

Yeah, in Python I often write things like the following when the comprehension doesn't fit on one line:

foo = [do_long_thing_with_x()
       for x in long_source_of_xs()]

The only time I find myself needing explicit line continuations in Python is for splitting string literals.

tgoyne avatar Dec 04 '13 20:12 tgoyne

I guess this is related: I am trying to construct a multiline string:

eq ('..(-(+(var[+a+], '
    ..     'var[+b+], '
    ..     '/(*(var[+c+], '
    ..         'var[+d+]), '
    ..       'var[+e+], '
    ..       'var[+f+])), '
    ..   '%(var[+g+], var[+h+])), '
    .. 'var[+i+])'),
  p0'a + b + c*d/e/f  - g % h .i'

, but playing with .. position here is not doing any good.

ZyX-I avatar Mar 10 '14 19:03 ZyX-I

You can use any operator to break to a new line now:

if hello == world and
    foo == bar
  print "yeah"

Not sure if that satisfies what you wanted. If my lines are long I tend to rewrite the code instead of wanting a line-break escape sequence

leafo avatar Dec 28 '15 10:12 leafo

@leafo Python has much better variant: unclosed parenthesis indicates that line should be continued allows placing operators where you want, and most coding standards I saw require placing all operators except comma on the same line as their right side, not left. Note that @tophf has also written and expr2 and not expr1 and \\, as well as I had .. expr2.

ZyX-I avatar Dec 28 '15 11:12 ZyX-I

Intuitively I would want an and/or operator to be the first thing on the new line rather than the last thing on the first line. That's how I've been writing them in Perl since back in the nineties (yes, I'm that old! :-)

bpj avatar May 25 '20 14:05 bpj