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:

satom99

satom99

Just to clarify, these are refered to as anonymous functions. Which, as mentioned, are nameless and should be called using a dot and may also be defined using the & shorthand. Refer to this lesson for a better overview.

NobbZ

NobbZ

This is very contrived but shows my point:

defmodule M do
  def foo(a) do
    foo = fn b -> a + b end
    # foo(1) # Nope, this will result in an infinite loop; also compiler might complain about unused variable `foo`
    foo.(1) # this works!
  end
end

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
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

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement