What are module and function names exactly? (they seem to be more than atoms)

Brushing up on Elixir after a couple of years, and want to properly understand its metaprogramming capabilities this time, but getting stuck. The 21. Optional syntax sheet is great (I don’t remember that chapter when I first learned Elixir, kudos!) but I still have some questions after experimenting in IEx.

I’m pretty sure I’m missing something basic and/or probably confusing certain constructs for others, but here it goes:

1. What does identifier M below expand to?

The statement found everywhere that module names are simply atoms is good to know, but it does not seem to be the whole story. From a new IEx shell:

iex(1)> defmodule(:M, [{:do, def(f, [{:do, 3}])}])

iex(2)> M.f

** (UndefinedFunctionError) function M.f/0 is undefined (module M is not available)
    M.f()

iex(2)> :M.f
3

iex(3)> quote do: :M.f
{{:., [], [:M, :f]}, [no_parens: true], []}


iex(5)> defmodule(M, [{:do, def(f, [{:do, 7}])}]) 

iex(7)> M.f
7

iex(8)> quote do: M.f
{{:., [], [{:__aliases__, [alias: false], [:M]}, :f]}, [no_parens: true], []}

iex(16)> M == :'Elixir.M'
true

I know that M is an alias of Elixir.M, which is the atom :'Elixir.M’ but what is happening with :M?

2. What does f expand to in the def/2 call?

There are some clues from the console, but it only shows a part of the picture:

iex(17)> defmodule(M, [{:do, def(:f, [{:do, 9}])}])

** (CompileError) iex:17: invalid syntax in def :f
    iex:17: (module)

iex(17)> defmodule(M, [{:do, def(f, [{:do, 9}])}]) 

{:module, M, <<...>>, {:f, 0}}

iex(18)> defmodule(M, [{:do, def(f(a,b), [{:do, 9}])}])

{:module, M, <<...>>, {:f, 2}}

Only after writing all this down did I realize that I may have never understood that M and f above are just identifiers (i.e., part of the syntax of Elixir) that have different AST representations, just like any other expression. Thus my questions may not make sense at all as M and f cannot be expected to be expanded into anything else as they are part of the syntax of the language.

iex(56)> quote do: ModuleNotDefined
{:__aliases__, [alias: false], [:ModuleNotDefined]}

iex(60)> quote do: non_existing_variable_or_function_name
{:non_existing_variable_or_function_name, [if_undefined: :apply], Elixir}

Thank you in advance for dispelling my confusion and any links to posts or the relevant source snippets is appreciated!

1 Like

:M is exactly :M.

iex(2)> M == :M
false

That is why you cannot call it with M.f().

2 Likes

f is also an atom.

iex> ast |> Macro.expand(__ENV__) |> Macro.to_string() |> IO.puts()                                           
alias M, as: nil, warn: false

:elixir_module.compile(
  M,
  {:__block__, [],
   [
     {:=, [],
      [
        {:result, [], Kernel},
        {:def, [context: Elixir, import: Kernel], [{:f, [context: Elixir], []}, [do: 3]]}
      ]},
     {{:., [], [:elixir_utils, :noop]}, [], []},
     {:result, [], Kernel}
   ]},
  [ast],
  __ENV__
)
1 Like

About 2nd point. def is expecting function call as a first argument. f is treated by Elixir the same as f(), but :f is not a function call (it is just literal atom). If you want to use atom directly then you need to do unquote(:f)() as a function name:

defmodule(M, [{:do, def(unquote(:f)(), [{:do, 9}])}])
4 Likes

Same question comfused me.

And I also comfused by why not def/2 return a Function.t, like the fn,but a tuple.

And I want to know if it possible defime variany length parameters function in Elixir, if refeine the def?

1 Like

def/2 is a macro, therefore it must return an AST

2 Likes

By my understanding defmodule essentially creates and loads a module within the beam. The module name is an atom and once the module is known to the beam you can use the atom name – the modules identifier – as a way to reference the module. Similarly functions are identified by an atom and doing Module.function() makes the beam try to fetch the module for the name :"Elixir.Module" and try to execute the exported function :function (with arity …/0) of that module.

All Module and function are is the identifiers to modules or functions, but not the module or the function implementation. Those are only known to the VM after the module has been loaded. There’s afaik no inherent connection between the module or function name (used in code) and an actual module with functions being loaded (or not) when the system runs.

That’s not possible on the beam. Functions are indentified by name and arity, so you cannot have an undefined arity. As explained above the code calling a functions kinda makes the VM “lookup” the implementation and that lookup does only support functions with a fixed arity.

3 Likes

I got it.

Function names are atoms, I know it also from Function.capture/3, the seconde is the function name, and it must be a atom.

1 Like