chrix75

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 :smile: ). 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 :blush:

Most Liked

cmkarlsson

cmkarlsson

Joe Armstrong called it :slight_smile:

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

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

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 :003:

Last Post!

NobbZ

NobbZ

I do use them as I need them and I like the erlang and elixir way.

In go code I write for work when I have a long function body in legacy code I often see foo() and then searching begins… My first reaction is scanning the list of functions on the right, but it’s not there. Then I scroll up to the current functions head to see if it is passed in as an argument, I don’t even find it there so I scroll even more up and see a dot imported package… I hate this ambiguity…

In elixir and erlang I can see at least if it is a variable or a “real” function.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
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