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

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement