EDIT: José confirmed on IRC that the behaviour I’ve reported in this post is a bug, so I created the issue on Github.
I’ve noticed some weird formatter behaviour in Elixir 1.6.4, which I personally don’t like. I’m not sure if it’s a feature or a bug, so I’m reporting it here.
From what I can tell, the formatter seems to respect my vertical choice of function parameters. So both of the following are fine:
Enum.reduce(foo, bar, baz)
Enum.reduce(
foo,
bar,
baz
)
This is cool, because in some cases, I like to break each param on a separate line, even if the line itself is not long.
Now, if the last param is a lambda, the formatter doesn’t respect the vertical choice. In other words, if I start with this code:
Enum.reduce(
foo,
bar,
fn el, acc -> baz(el, acc) end
)
It’s always turned into
Enum.reduce(foo, bar, fn el, acc -> baz(el, acc) end)
That becomes somewhat annoying when the lambda is multiline, because my code is always force formatted into
Enum.reduce(foo, bar, fn el, acc ->
baz(el)
qux(acc)
end)
Personally, I have problems when reading this layout, because the lambda arguments are further right than the body.
This becomes particularly annoying in the following case:
Enum.reduce(
moderately_complex_expression,
moderately_complex_expression,
fn element, accumulator ->
do_something(element)
do_something(accumulator)
end
)
The code gets reformatted into:
Enum.reduce(moderately_complex_expression, moderately_complex_expression, fn element,
accumulator ->
do_something(element)
do_something(accumulator)
end)
Which I find quite terrible to read.
Of course, the code can be improved by using temp local vars and/or named functions, but in some cases I just prefer to inline everything. Sadly, given the formatted code above, inlining is not an option anymore.
So my question would be: is such formatting intended or is this a bug?
cc @josevalim