vladic4t
Elixir definition of functions without parenthesis
Since Elixir’s type system is becoming a new topic, can I take the chance to request a feature to be introduced around functions?
Namely, to be able to define functions without parenthesis around its arguments.
def sayhi "world", do: "Hello" <> " world!"
def sayhi("world"), do: "Hello" <> " world!"
Why?
For simplicity. Elixir is mainly a declarative programming language, not imperative. It is a very flexible language and those parenthesis can be left to erlang’s rigid syntax. And anyway let anyone adopt the convention he best finds. I find functions without parenthesis clearer.
Why not?
let me know! ![]()
Most Liked
dorgan
Your example would be ambiguous, is , do: ... an argument to def or to sayhi?
Essentially it can be interpreted in two ways:
def(sayhi("world", do: "Hello" <> " world!"))
def(sayhi("world"), do: "Hello" <> " world!")
It’s the same reason the lack of parenthesis make pipeline expressions ambiguous, for example:
foo bar, baz |> IO.inspect
Can be
foo(bar, baz) |> IO.inspect()
foo(bar, baz |> IO.inspect())
And there’s no way for the compiler/parser to tell which one do you mean
Sebb
You should try Lisp. ![]()
dorgan
def and similar are macros, they abide by the same rules as any other macro, so the compiler doesn’t “know” it’s a function definition until the macros are fully expanded.
There are also cases where the function definition has no body:
def foo(bar, opts \\ [])
def foo(:a, opts) do ... end
def foo(:b, opts) do ... end
al2o3cr
This is specifically a limitation of the single-line , version of def, the multiline version is fine without parens:
def sayhi "world" do
"Hello" <> " world!"
end
The tricky part of supporting that syntax in single line is context-sensitivity - the comma’s meaning in this definition changes depending on the tokens that follow it.
def sayhi_with_kwarg "world", do: "Hello" <> " world!"
# versus
def sayhi_with_kwarg "world", do: "Hello" <> " world!" do
:ok
end
Before the second do is seen, the , separates the arguments from the definition. After, it delimits the first argument from the second.
bmitc
Welcome to the forum!
Regarding this feature, I personally would rather this not be available. Elixir already has many cases where parentheses are optional, and I feel this needlessly adds “optionality” into the language such that everyone does it different. In fact, I use Credo to enforce that parentheses are included when defining functions with no arguments. The reason is that otherwise, they end up looking like properties or variables or something, when they are still functions and must be called with parentheses when used anyway.
In fact, a lot of official documentation and books have code without parentheses that have them put back by the official Elixir formatter, usually revolving around the use of macros. I feel that’s a showcase where allowing no parentheses in the first place is not really even worth it.
Lastly, there are some weird issues with leaving off parentheses and pipelines. I don’t think Elixir is really built with a no-parentheses style in mind.
iex(3)> 2 |> Kernel.+ 34
warning: parentheses are required when piping into a function call. For example:
foo 1 |> bar 2 |> baz 3
is ambiguous and should be written as
foo(1) |> bar(2) |> baz(3)
Ambiguous pipe found at:
iex:3:3
36








