marick

marick

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.

Showing Posts 1 to 2

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.

— All posts loaded —

Where Next? Top

Trending in Chat/Questions Top

Other Trending Topics Top

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
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews