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?
Top comments (3)
Indeed, its uncommon pattern to use anonymous functions as variable and also doesn't bring significant code readability and is against maintainability.
Yes I think you are. It's a very rare pattern in our neck of the woods and after a single moment of friction EVERYONE gets it and moves on.
You know what my motivation for that post was? José Valim was expecting a question about the dot here:
18:27 "is it the dot?" https://www.youtube.com/watch?v=2Vhd5fw59UY&t=1107s
19:06 Regarding the dot... https://www.youtube.com/watch?v=2Vhd5fw59UY&t=1146s