vladic4t

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! :slight_smile:

Most Liked

dorgan

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

Sebb

You should try Lisp. :grin:

dorgan

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

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

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

Where Next?

Popular in Discussions Top

matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New
axelson
Decided against including more info in the title, but the gist is that Plataformatec sponsored projects will continue with the assets bei...
New
fireproofsocks
This is more of a general question, but I’m wondering how other people in the community think about the pattern matching in function sign...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
hazardfn
I suppose this question is effectively hackney vs. ibrowse but we are at a point in our project where we have to make a choice between th...
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement