multiline, operators and force one expresion

348 views
Skip to first unread message

Jose Luis

unread,
Aug 11, 2014, 3:20:13 AM8/11/14
to elixir-l...@googlegroups.com
Hi.

Suppose we have long equations and we want to split in multiple lines.


we would write something like...

(
2
*
3
)

or...

if true  do
  2
  *
  3
end

iex(11)> quote do
...(11)> 2
...(11)> *
...(11)> 3
...(11)> end

{:*, [context: Elixir, import: Kernel], [2, 3]}




But replacing * by + operator, it doesn't work same way

(
2
+
3
)


iex(10)> quote do 
...(10)> 2
...(10)> +
...(10)> 3
...(10)> end

{:__block__, [], [2, {:+, [context: Elixir, import: Kernel], [3]}]}


+ is been parsed as unary operator and the code is splitted in two expressions

iex(12)> quote do
...(12)> 2;
...(12)> +3
...(12)> end
{:__block__, [], [2, {:+, [context: Elixir, import: Kernel], [3]}]}


It's equivalent to 2; +3


Obviously with function notation, we can split it without ambiguities

iex(36)> (Kernel.+ 1,    
...(36)> (Kernel.* 2,
...(36)> 3))



Is there anyway to force single expression on multiple lines?


kind regards

José Valim

unread,
Aug 11, 2014, 3:33:22 AM8/11/14
to elixir-l...@googlegroups.com
The only way is to hint the current expression has not finished by adding \ at the end of the line.

iex> 1 \
...> + 2
3

The feature seems to be broken on IEx right now (I will push a fix soon) but it works in files.

Matt Gushee

unread,
Aug 11, 2014, 4:23:14 AM8/11/14
to elixir-l...@googlegroups.com
Hi, Jose--

Does every term or operator have to be on a separate line? Or can you
write operators at the ends of lines, like:

> 5 +
> 17
22

The above code works for me, at least.

--
Matt Gushee
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elixir-lang-ta...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jose Luis

unread,
Aug 11, 2014, 1:02:05 PM8/11/14
to elixir-l...@googlegroups.com
Writting the operator at the end of the line is great. Writting \ to inform the expression continues on next line is even better.

Semicolon and new line, some times means new expression and sometimes not.

It looks a bit confusing to me

" In Elixir, expressions are delimited by a line break or a semicolon ;."

Here we have two expressions separated by line break...

a = if true do
2
+3
end
a==3


Here we have one expression with a line break inside

a = if true do
2
*3
end
a==6

Onorio Catenacci

unread,
Aug 11, 2014, 1:50:50 PM8/11/14
to elixir-l...@googlegroups.com
Ok, so if you've got an expression that short (2 * 3) then why would you break it over multiple lines?  And if you've got an expression significantly longer ( (2 * 3) * (8 * 9) + (4 / 3)  . . . ) then I'd consider using subexpressions.  That is,  a = (2 *3), b = (8 * 9), c = a * b  d = ( 4 /3 ) ,, e = c + d etc. It will simplify the code, make it less error prone (both to write initially and to maintain) and it gets you nicely around this issue.  

Speaking only for myself one of the smelliest code smells I know of is trying to do too much in one place.  45 line if statements (I used to work on C++) are pretty much impossible to maintain. 

--
Onorio

Jose Luis

unread,
Aug 12, 2014, 2:08:49 AM8/12/14
to elixir-l...@googlegroups.com
Onorio, probably you are right

In any case I love easy and clear rules


" In Elixir, expressions are delimited by a line break or a semicolon ;."


This is an easy and clear rule. But looks it isn't fully true and the real rule has to be more complex

In next case, we have an expression even with a line break (the same can be done with a semicolon).

a = if true do
2
*3
end
a==6


Here, I expected a compilation error *3 is not a valid expression.


I love elixir, but this looks weird to me.



José Luis

José Valim

unread,
Aug 12, 2014, 4:16:40 AM8/12/14
to elixir-l...@googlegroups.com
" In Elixir, expressions are delimited by a line break or a semicolon ;."

This is an easy and clear rule. But looks it isn't fully true and the real rule has to be more complex

Yes, you are right. A line break leaves the expression still open.

In next case, we have an expression even with a line break (the same can be done with a semicolon).

The semicolon one is a bug. I will fix it in master soon.

In any case, I think it is worth discussing why we have this feature. The assumption is that Elixir started with a very regular syntax, why did we add this behaviour then?

It happens that everyone has probably used this behaviour but with a different operator: 

foo
|> bar
|> baz

It is the same idea. If the line below starts with a binary operator, it becomes a continuation of the first line. The only two exceptions are + and -, which are also unary operators (and that is one of the reasons I have rejected some proposals where other existing binary operators can also be used as unary).


Jose Luis

unread,
Aug 12, 2014, 8:49:31 AM8/12/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Thanks José.

This is very logical and clear.


impressive work
Reply all
Reply to author
Forward
0 new messages