chrix75
Why do we use a dot to call a function?
Hi,
I’ve started learning Elixir by reading the book Programming Elixir and I’ve got one question (at this moment
). What is the motivation for using the . character to call a function ?
It’s strange to write myFunc.() instead of myFunc(). I suppose I find that weird because of my other languages knowledge ![]()
Most Liked
cmkarlsson
Joe Armstrong called it ![]()
I quote:
“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.”
NobbZ
You do use the dot for functions bound to variables. Not for defined functions,
iex(1)> defmodule M do
...(1)> def foo(a), do: a + 1
...(1)> end
# ...
iex(2) foo = fn a -> a + 1 end
# ...
iex(3) M.foo(1)
2
iex(4) foo.(1)
2
This is mainly to distinguish in case of nameclashes.
AstonJ
The dot is only used when calling anonymous functions that have been bound to a variable (and not functions defined inside a module). The dot also reminds us that it is an anonymous function.
This is an anonymous function:
foo = fn a -> a + 1 end
And called like this:
foo.(10)
This is a function defined inside a module:
defmodule MyModule do
def foo(a), do: a + 1
end
And called like this:
MyModule.foo(10)
If you continue reading the book Dave goes on to explain this ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









