mruoss
Hi community!
What is the correct way to implement the __using__ macro with an option that changes the way generated functions behave? Or is that a no-go by itself?
An example:
defmodule UsedModule do
defmacro __using__(opts) do
quote do
if Keyword.get(unquote(opts), :with_extra, false) do
def extra(), do: "with extra"
else
def extra(), do: "no extra"
end
end
end
end
defmodule UsingModuleWithExtra, do: use UsedModule, with_extra: true
defmodule UsingModuleNoExtra, do: use UsedModule
iex> UsingModuleWithExtra.extra()
"with extra"
iex> UsingModuleNoExtra.extra()
"no extra"
My question: Is this the way to go inside the __using__ macro? Or would you define the extra function once and do the if inside like this?
def extra() do
if Keyword.get(unquote(opts), :with_extra, false),
do: "with extra",
else: "no extra"
end
Or is there a cleaner way I’m not seeing?
Thanks!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
In case you have a static output like in this example then you can simply call
unquote(string)and outsidequoteblock setstringvariable based on option.In other case I would recommend to simply move
ifcondition outsidequoteblock and simply use 2quotecalls.Note: If
opts[:with_extra]does not exists then you gotnilwhich is falsy value and therefore we do not need aKeyword.get/3call here.Also Liked
Eiji
Yeah, I can say that it may depend on case. For example there is a big difference working on raw data passed to macro and variables passed to macro even if their declaration was static one line above …
unquoteinsidequoteblock solves the problem. However in some cases you may expect some value to be passedrawinstead.Here since the value is known “outside” the half of code we generate would never be used.
A simple example shows how important it may be:
Look that if we would have only 1/10 features enabled then we would generate only one
quoteblock.However if we would write it like this:
then said module with one enabled feature would have generated extra loop with 9/10 skipped elements. Also in example above
apply_feature/1is a public macro for absolutely no reason (comparing to previous example).Also it would be nice to provide a link for mentioned text as José could talk about other case or just more complex example. It would be good for others to see all sides of this coin.
Eiji
If it’s stupid, but it works then it’s no stupid!
I have used
atomas it’s best for what I calledfeature_nameas those inapply_feature/1are known at compile-time. You can use strings, integers and every other data that you can write pattern-matching for.Also take a look at example below:
If you want to call
String.to_atom/1inside macro then:Enum.map(list, &String.to_atom/1)as long as you require list of atoms passed as raw list or sigil (see above example)String.to_atom/1call needs to be passed insideunquote/1call.moduleandfunctionblock you can useString.to_atom/1call as well insideunquote/1call as outsideunquote/1call.Hope that helps.
Last Post!
Eiji
You can’t expand them, because unlike
sigilthey arespecial forms. However if you still expect to pass rawmapthen you can solve that in few ways depends on use case …If you do not want to mix your code with handling special forms then all you need to do is write something like: