iex> if true, do: (
...> a = 1 + 2
...> a + 10
...> )To me, the discussion around omitting parens from function calls is an unfortunate side-effect of the implementation of Elixir's sugar for blocks. Strictly speaking, `if` is a macro and every time you write.
--
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-co...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
--
The `,` after an `if` is required if you write `if foo, do: something` and is forbidden if you write `if foo do\n...something...\nend`. I understand why this is, but it isn't the clearest thing to a newcomer. Then, it is OK to add line breaks in the 1st case, but they are mandatory in the 2nd case. One can't write `if coming then "Hello" else "Goodbye" end`.
Another not so favorite gotcha: `[ foo: bar ]` is fine and means [ { :foo, bar } ]`. Also `[ foo, bar | baz ]` is fine and is the same as `[ foo, bar ] ++ baz`, except that its cleaner and more efficient. But try to write `[ foo: bar | baz ]` and you get "undefined function: IEX.Helpers.|/2".
I expect other people have noticed other such rough spots in the syntax... Even just listing them may be useful.
> I'm torn between writing a book which always uses the parentheses, because they generally add "what's going on here" clarity for beginners, and a book which uses them as rarely as possible, because the use of parentheses seems to be a marker for "a newbie wrote this." (At least it frequently runs that way in Ruby culture.)
This really is not correct - in Ruby parenthesis are idiomatic for method calls unless the method is a part of a DSL or is an attribute accessor as well as a few other circumstances. https://github.com/bbatsov/ruby-style-guide
What I have observed in most Elixir code out there is that people nearly always use parenthesis when calling functions, although most of the examples in the standard library do not use them. I'm not sure why the docs look like that to be honest.
Considering Elixir mostly draws on syntax and conventions from Ruby and Erlang, probably we can expect that trend to continue. Most other functional languages have different conventions though. The ML-family languages like Haskell, OCaml, F# do not use parenthesis in this way, and in Scala where they are optional you see a mix of styles but it seems more idiomatic not to use parens unless they are really needed for precedence or clarity. While LISP expressions are always in parens the function call and arguments are delimited only by whitespace.
In Ruby there are many guidelines, people have different approaches when it comes to parenthesis. I don't have any. That's why the docs on stdlib are inconsistent, as Jeremy pointed out. If someone wants to work on those docs and make them abide the rule above, I wouldn't mind the consistency.
I will be happy to submit a PR for this.