toraritte

toraritte

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!

Showing Posts 1 to 7

Nicd

Nicd

:M is exactly :M.

iex(2)> M == :M
false

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

eksperimental

eksperimental

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__
)
hauleth

hauleth

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}])}])
keep_zen

keep_zen

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?

eksperimental

eksperimental

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

LostKobrakai

LostKobrakai

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.

keep_zen

keep_zen

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews