What is the definition of a remote function?

When we try to evaluate an Elixir code m = %{}; m.foo = 1 on IEx, it emits the following error message :

** (CompileError) iex:1: cannot invoke remote function m.foo/0 inside a match

I know that we cannot call m.foo/0 inside a pattern match, but I have some questions:

  • What is a remote function exactly?
  • What is called a function that is not a remote function?

I cannot find a definition of this word within the documentation.

Perhaps, it refers to a remote call, which is used in the left . right?

A remote function is a function in a module that isn’t this one.
A local function is one defined in the current module.

There is a slight cost in the BEAM to execute a remote function over a local one, but not material to a design decision.

6 Likes

A remote call is every function call which is qualified by its module.

defmodule M do
  def f(0), do: 0
  def f(n) when n > 0, do: M.f(n-1) + 2
end

The recursive call here is a remote call, with all of its costs.

3 Likes

Could you suggest some resource to learn about the costs of remote calls please? At this point I’m intrigued and curious! Thank you.

1 Like

That is not fully true as __MODULE__.foo() is also remote call. In short the difference between remote and local call is that you put the module name before remote call.

3 Likes

The advantage and disadvantage of the remote calls is exactly the same:

The remote call allows for the hot code reload, which mean that it need to check if there is no newer code each time it is called.

However if you are trying to use local call as a lambda in form of &foo/0 then it will be slower than &__MODULE__.foo/0 as the second can be as a constant so will be faster to call.

1 Like

Though here we have to remember, that for &__MODULE__.foo/0 to work, the function needs to be exposed/public, by using def, while when doing &foo/0 the function is allowed to be private (defp).

2 Likes

Any remote call need to point to public function.

3 Likes

Thank you very much @hauleth and @NobbZ for the explanation! Is this kind of things (I mean ignorance here) that triggers the impostor syndrome in me… than I remember It’s all in my mind. Nothing serious indeed. :sweat_smile:

1 Like

Thank you everyone for answering the question.

Does anyone know where to find a description of a remote function call in the erlang documentation or its source code?

I think this is a good starting point:

http://erlang.org/doc/reference_manual/expressions.html#function-calls

2 Likes

That’s it. Great.