moonscript
moonscript copied to clipboard
Line continuation character
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.
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
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.
Table comprehension expressions could also benefit from this sometimes.
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.
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.
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 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.
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! :-)