fireproofsocks

fireproofsocks

Passing options to module

I’m trying to pass some options to a module when I use it, but anything beyond a simple list seems to get quoted (?) and arrives in abstract tree syntax (AST) format. Forgive the OO terminology, but here’s an example of the issue:

defmodule MyChild do
   use MyParent, default_sort: %{updated_at: -1}
end

Then, in MyParent:

defmodule MyParent do
   defmacro __using__(opts) do
     IO.inspect(opts)
    # ...
   end
end

The output of the inspection is something like this:

[
  default_sort: {:%{}, [line: 20], [updated_at: {:-, [line: 20], [1]}]}
]

What’s going on there?

Specifically, I would like to create a module attribute in the __using__ block so that all of the functions provided in the __using__ block and any functions in the calling MyChild module can reference it. But so far, the only solution I’ve found is to declare the module attribute before the use statement, e.g.

defmodule MyChild do
   @default_sort %{updated_at: -1}
   use MyParent
end

That works, but it feels slightly weird to have the module attribute before the use.

Thanks for any insights.

Most Liked

kip

kip

ex_cldr Core Team

Macros always receive AST and are always required to return AST. So what you are seeing is normal. As the guide says:

… However, as we learned in the previous chapter, the macro will receive quoted expressions, inject them into the quote, and finally return another quoted expression.

I typically find the following construct useful for dealing with this when I want to use the macro arguments at compile time (and with @before_compile delegate the production of the final AST to a regular function).

bind_quoted is recommended in most cases and takes care compile-time unquoting. If you are passing something like a map you will have to Macro.escape/1 it first. The example below is from ex_cldr so its just an example. But you can see it is setting a module attribute.

  defmacro __using__(opts \\ []) do
    quote bind_quoted: [opts: opts] do
      @cldr_opts opts
      @before_compile Cldr.Backend.Compiler
    end
  end

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement