marick

marick

Need for a how-to for writing `def f(...)...end` code using macros?

When I started writing macros that define named functions with def, I didn’t find any blog posts etc. that were devoted to the topic. Are there any? Should I write one? There are, I think, some special gotchas that beginners should be able to find all in one place.


I’m referring to macros like one I recently wrote:

  defmacro defaults_may_be_overridden(pairs) when is_list(pairs) do
    for pair <- pairs, do: Here.def_optional(pair)
  end

  def def_optional({name_atom, default}) do
    function_name = {name_atom, [], nil}
    quote do
      def unquote(function_name),
        do: get_env(unquote(name_atom), unquote(default))
    end
  end

That’s used to create a bunch of configuration getters functions while omitting unnecessary words:

  defaults_may_be_overridden(
    reload_timeout: 150,
    logging_enabled: true,
    reload_callback: constantly(:irrelevant_return_value),
    load_first: false,
    src_dirs: []
  )

There are issues like:

  • defmacrop may surprise you because it doesn’t create macros available at function-definition scope.
  • Converting between function name ASTs and atoms is something you have to learn.
  • If you want to write a defXXX macro, you may want to handle guards, and the AST for guards was surprising to me.

Most Liked

Sebb

Sebb

I don’t know if there are any, but I can tell you, that I was in a similar situation (Behaviours, defoverridable and implementations - reloaded) and did not find anything.

Absolutely. If you’ve just understood the topic, you’re most qualified to write about it (you still understand how it is to not understand).

dorgan

dorgan

I think it would be nice, I think there are many articles showcasing how to build a particular macro, but they also kind of expect you to experiment yourself to find out all the different patterns you can find and produce(I’m guilty of this as well :slight_smile: ).

While it’s not particularly difficult to find those patterns, I think it would be nice if all that experimentation was documented in a single place, and a how-to article sounds like a great step forward.

Where Next?

Popular in Chat/Questions Top

New
Iex.new
Hello!, I just started this week to discover Elixir. I’m a PHP-Programmer and did some sutff in Go too. The more I read about Elixir t...
New
boddhisattva
Greetings everyone, At my current workplace we’re evaluating different ways of building a microservices architecture for some parts rela...
New
Kielo
Hi, I run a language learning blog and would like to learn how to code so I can create an app to help English speakers learn French. I ...
New
asfand
Hi Everyone, I am a student and know basics of web development, used some php and ruby, but I am not an expert in any. I want to learn E...
New
TwistingTwists
I want to learn DSL. Don’t know how to write one. What;s the best introductory resource? I see some macro being used here. Is DSL only ...
New
Chawki
Hi,i’m new to elixir. i’m searching elixir small programs to try it out my self,Is any good resources out there? Thank you.
New
pdgonzalez872
Hi! In my quest of becoming the best Elixir dev I can be, I saw one aspect in my career that I’d like to improve upon. This is language...
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
Besto
I’ve been trying to start learning Elixir for a couple of weeks while I develop a tiny project I have on node.js, but every time I try to...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement