chrix75

chrix75

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:

Showing Posts 1 to 10

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.

chrix75

chrix75 OP

I don’t get the point :confused: Sorry.

When could we have any problem calling foo(1) in place of foo.(1) ?
From your code, is it for the case we have a function bound to foo variable inside the module M ?

Thanks

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

AstonJ

AstonJ

This is also a great point - the name of the variable isn’t the name of the function:

foo = fn a -> a + 1 end

Here, foo is just the name of the variable that the anonymous function has been bound to - the function itself is nameless/anonymous.

Named functions always go inside a module:

defmodule MyModule do
  def foo(a), do: a + 1
end

Here, the name of the function is foo.

Specifically, we call this foo/1 where 1 is the arity (the number of arguments the function takes)

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.”

AstonJ

AstonJ

Haha, I have to admit that it was one of the things that I wondered myself - but I think that is also true of other things that are different to what we’re used to (or for some reason or another, feel peculiar to us). Once we learn about them it just makes sense and we move on.

With that said, I have been thinking about whether we could do with a Frequently Asked Questions section, as well as perhaps a Glossary section - for common stuff like this. We can then easily direct people to the associated thread whenever the topic comes up (with the added benefit them many people will browse such sections as well) :003:

jeremyjh

jeremyjh

It has already been explained in this thread but for additional context this early thread may help as well. I do not think anyone mentions it directly but it is also important to know two things:

  1. Elixir code compiles to Erlang AST
  2. Erlang solves this ambiguity problem by requiring capital letters for variable names and lowercase for atoms (including module and function names). So there is no chance of ambiguity here.

Elixir solves the ambiguity between variable and atom names by prefixing atoms with a colon but including a colon in local function invocation seems like a worse trade-off than a dot for anonymous functions.

I think it is a shame though and easily the biggest remaining wart in the language, and the cost of it is probably a diminished usage of anonymous functions in everyday code.

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews