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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Nicd
:Mis exactly:M.That is why you cannot call it with
M.f().eksperimental
fis also an atom.hauleth
About 2nd point.
defis expecting function call as a first argument.fis treated by Elixir the same asf(), but:fis not a function call (it is just literal atom). If you want to use atom directly then you need to dounquote(:f)()as a function name:keep_zen
Same question comfused me.
And I also comfused by why not
def/2return a Function.t, like thefn,but a tuple.And I want to know if it possible defime variany length parameters function in Elixir, if refeine the
def?eksperimental
def/2is a macro, therefore it must return an ASTLostKobrakai
By my understanding
defmoduleessentially 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 doingModule.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
Moduleandfunctionare 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
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.