Proposal: allow newline after `with`

63 views
Skip to first unread message

Po Chen

unread,
Jan 30, 2018, 7:07:24 AM1/30/18
to elixir-lang-core
x = %{x: 1}

with %{x: x} <- x do
  IO
.inspect x
end

^ This works.

x = %{x: 1}

with
 
%{x: x} <- x
do
  IO
.inspect x
end

^ This is a syntax error.

Proposing that the latter should be allowed for 2 reasons:

1) it looks like it should work ¯\_(ツ)_/¯
this sounds like trolling but is really not. the syntax that looks valid should be valid.

2) it is better, more consistent for indentation and code formatting.

with %{a: a} <-
       some_really_long_line_about_a
,
     
%{b: b} <-
       some_really_long_line_about_a
do

 
...
end

^ this is what it would look like when formatted with the current syntax rules
  • note the indentation of line 2 is 7 spaces
  • and line 3 is indented 5 spaces

if newline is allow after `with`

with
 
%{a: a} <-
    some_really_long_line_about_a
,
 
%{b: b} <-
    some_really_long_line_about_a
do

 
...
end

^ it would look like that
  • everything is indented consistently, 2 spaces and 4
  • no need to waste extra horizontal space
  • also makes it easier for text editors to auto indent without having trouble with abnormal indent sizes

José Valim

unread,
Jan 30, 2018, 7:20:49 AM1/30/18
to elixir-l...@googlegroups.com
Imagine you have this code:

   foo = 1
   bar = 2
   foo bar do
     :ok
   end

Would you expect it to be equivalent to:

   foo = 1
   bar = 2
   foo
   bar do
     :ok
   end

It isn’t because “foo” in its own line is a valid expression. Replace “foo” by “with” and you can see why your proposed syntax doesn’t work.

If you want to write it as you proposed, you need to use parens:

   foo = 1
   bar = 2
   foo(
     bar
   ) do
     :ok
   end
--


José Valim
Founder and 
Director of R&D

Po Chen

unread,
Jan 30, 2018, 7:39:07 AM1/30/18
to elixir-lang-core
Ah I thought `with` was part of the language, then I realized how small the language core is.
Now I wish the language is actually a little bigger :S mixed feelings.

Thanks for the reply!

Ben Wilson

unread,
Jan 30, 2018, 7:46:15 AM1/30/18
to elixir-lang-core
This is achievable with `\` btw:

with \
  %{x: x} <- y
do
  x
end

José Valim

unread,
Jan 30, 2018, 7:55:58 AM1/30/18
to elixir-l...@googlegroups.com
Good call Ben! The formatter will rewrite it though, while it does keep parens.



José Valim
Founder and 
Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/a644c670-f090-4eaf-a0df-fa47333519b8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

OvermindDL1

unread,
Jan 30, 2018, 10:08:23 AM1/30/18
to elixir-lang-core
I'm still quite curious why `with` is not entirely in the body like `cond` and `case` and so forth is (ditto with `for`), so it'd be:

```elixir
with do
  %{x: x} <- y
  x
end
```

It follows the pattern of cond and so forth, no weird comma's needed, no weird prefix `do:` or so forth, you only need to split on `<-`, etc....
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

José Valim

unread,
Jan 30, 2018, 11:13:41 AM1/30/18
to elixir-l...@googlegroups.com
In the with proposal we explain that "with" was meant to mirror for-comprehensions. It has been also discussed that nowhere in Elixir we traverse the body retrieving expressions and that's not a precedent we want to introduce in the core language itself.



José Valim
Founder and 
Director of R&D

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/bbf54f61-ffc4-46e3-b69f-5bbc03f0df70%40googlegroups.com.

Po Chen

unread,
Jan 30, 2018, 6:27:48 PM1/30/18
to elixir-lang-core
Hi Jose,

I just tried going with parens, the formatter does not seem to keep parens either in this case..

José Valim

unread,
Jan 30, 2018, 6:38:17 PM1/30/18
to elixir-l...@googlegroups.com
Please open up a bug report!



José Valim
Founder and 
Director of R&D

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/3831e575-c289-4e37-9171-de95b8ae2477%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages