Erlang/OTP 19 [erts-8.0.2] [source-753b9b9] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Elixir 1.3.2
The with statements don't compile if in the following form (taken from a gist I made that expounds the different syntax permutations using with):
# Doesn't compile
test "with, comma-do, newline, backslash, comments" do
:ok =
with \
# Comment here <<<< Won't compile because of this line
:ok <- :ok,
:ok <- :ok,
do: :ok
end
# Doesn't compile
# test "with, do block, newline, backslash, comments" do
:ok =
with \
# Comment here
:ok <- :ok,
:ok <- :ok do
:ok
end
end
I thought this was syntactic side-effect, but when I mentioned this behavior on the elixir forum, @josevalim asked me to open an issue for it.
Include code samples, errors and stacktraces if appropriate.
It should compile, regardless of if the with \ line is followed directly by a comment or not.
Thanks for all of y'alls hard work! :+1:
I made another quick test and the following also does not work:
IO.puts \
# some comment
:bug
This fails trying to call IO.puts/0, just like if code were
IO.puts # some comment
:bug
This got me thinking on what the proper behavior should be for an escaped newline followed by a comment. Made a couple of examples on other langs just to get an example example on how they handle this case:
Ruby calls puts() and then just evaluates the symbol
puts \
# comment
:foo
Bash is pretty much the same, echoes nothing and then tries to evaluate foo which fails when undefined.
echo \
# comment
foo
So now I'm wondering, if we should handle this case specially in Elixir or if the current parser is actually doing the _right thing_.
I do think that \ means "don't consider the following newline as a expression separator such as newline or ;". In such cases, this:
IO.puts \
# some comment
:atom
would be interpreted (by just ignoring - that is, removing, the newline) as:
IO.puts # some comment
:atom
Ah that's interesting. I was just looking at it from the usage POV. This framing of the issue does seem to change things to me, and also I have become quite comfortable with (and fond of) using paren syntax:
with(
# some comment
x <- foo()
...
) do
{:ok, x}
else
error -> handle(error)
end
Yes, \ is literally about escaping the upcoming newline (that's why it is at the end of the line and right before the new line). So I believe we can close this.