Why I think the dot is not a problem in Elixir
Adolfo Neto
Posted on April 27, 2022
Update! Read this beautiful post by José Valim: Why the dot (when calling anonymous functions)?
The Elixir community knows that, in Elixir, when you define an anonymous function and assign it to a variable, you have to use a dot (.) to call it:
iex(1)> double = fn x -> x*2 end
#Function<44.65746770/1 in :erl_eval.expr/5>
iex(2)> double.(3)
6
iex(3)>
This doesn't happen in Erlang:
1> Double = fun(X) -> X*2 end.
#Fun<erl_eval.44.65746770>
2> Double(3).
6
3>
You don't have to put a dot between Double
and (3)
in Erlang.
But you have to put a dot at the end of each line.
The late Joe Armstrong, one of the creators of Erlang, wrote in 2013, when he spent "A Week with Elixir," more specifically in the section "Funs have an extra dot in the name":
In school I learned to call functions by writing f(10) not f.(10) – this is “really” a function with a name like Shell.f(10) (it’s a function defined in the shell) The shell part is implicit so it should just be called f(10).
If you leave it like this expect to spend the next twenty years of your life explaining why. Expect thousands of mails in hundreds of forums.
I believe this didn't happen. One or other person complains here and there, but not "thousands".
Why? My opinion is that Elixir developers usually don't assign an anonymous function to a variable. That's why this is not a big problem. Am I right?
Posted on April 27, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.