# \#defmacro

**URL:** https://forum.elixirforum.com/tag/defmacro/2393.md

[Latest](https://forum.elixirforum.com/latest.md) · [Categories](https://forum.elixirforum.com/categories.md) · [Tags](https://forum.elixirforum.com/tags.md)

---

## [Introduce a PHP-style \`compact\` function](https://forum.elixirforum.com/t/introduce-a-php-style-compact-function/71308)

<div class="topic-metadata">

**Author:** [@MeerKatDev](https://forum.elixirforum.com/u/MeerKatDev)\
**Replies:** 3\
**Last updated:** [June 16, 2025, 1:56pm UTC](https://forum.elixirforum.com/t/introduce-a-php-style-compact-function/71308 "2025-06-16T13:56:48Z")

</div>

many times we do stuff like (e.g. test setups, json views in phoenix) aaa = ... bbb = ... ccc = ... %{aaa: aaa, bbb: bbb, ccc: ccc} and would be cool to have a standard macro doing the same with compact(aaa, bbb, ccc)…

---

## [Adding guard to macro that just calls another function](https://forum.elixirforum.com/t/adding-guard-to-macro-that-just-calls-another-function/69561)

<div class="topic-metadata">

**Author:** [@roflbobl](https://forum.elixirforum.com/u/roflbobl)\
**Replies:** 7\
**Last updated:** [February 24, 2025, 11:35am UTC](https://forum.elixirforum.com/t/adding-guard-to-macro-that-just-calls-another-function/69561 "2025-02-24T11:35:50Z")

</div>

I have a problem, where i have a macro the calls another function. I want to be able to add optional guards to my macro, and call the nested function with those guards. I have a solution, but i feel like it is verbose an…

---

## [Macro to provide timing for a function call - handle any signature](https://forum.elixirforum.com/t/macro-to-provide-timing-for-a-function-call-handle-any-signature/69381)

<div class="topic-metadata">

**Author:** [@MadsBoydMadsen](https://forum.elixirforum.com/u/MadsBoydMadsen)\
**Replies:** 29\
**Last updated:** [February 14, 2025, 12:45pm UTC](https://forum.elixirforum.com/t/macro-to-provide-timing-for-a-function-call-handle-any-signature/69381 "2025-02-14T12:45:44Z")

</div>

I’m working on a macro to support automatic timing for functions. If I have this - say: def double(x) do x + x end I would like to be able to change it to this on a needs basis: def\_timed double(x) do x + x end I…

---

## [Does adding a dummy guard \`when true\` affect performance](https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151)

<div class="topic-metadata">

**Author:** [@lud](https://forum.elixirforum.com/u/lud)\
**Replies:** 4\
**Last updated:** [December 18, 2024, 9:50am UTC](https://forum.elixirforum.com/t/does-adding-a-dummy-guard-when-true-affect-performance/68151 "2024-12-18T09:50:51Z")

</div>

In order to only have one branch of code in a macro that defines functions with support for guards, I would like to always have a guard. So I want to define the generated functions as def myfun(arg1,arg2) when true do …

---

## [BinStruct - library for writing declarative code to get rich generated set of binary parsing/encoding features](https://forum.elixirforum.com/t/binstruct-library-for-writing-declarative-code-to-get-rich-generated-set-of-binary-parsing-encoding-features/67639)

<div class="topic-metadata">

**Author:** [@Ridtt](https://forum.elixirforum.com/u/Ridtt)\
**Replies:** 12\
**Last updated:** [November 26, 2024, 11:26am UTC](https://forum.elixirforum.com/t/binstruct-library-for-writing-declarative-code-to-get-rich-generated-set-of-binary-parsing-encoding-features/67639 "2024-11-26T11:26:27Z")

</div>

Hex: bin\_struct | Hex Git: GitHub - 4ait/bin\_struct · GitHub Docs: bin\_struct v0.2.34 — Documentation BinStruct is a library which main function is to convert readable as possible declarations to robust and performant…

---

## [Why is the variable not available inside the macro?](https://forum.elixirforum.com/t/why-is-the-variable-not-available-inside-the-macro/66542)

<div class="topic-metadata">

**Author:** [@budgie](https://forum.elixirforum.com/u/budgie)\
**Replies:** 3\
**Last updated:** [October 6, 2024, 6:12am UTC](https://forum.elixirforum.com/t/why-is-the-variable-not-available-inside-the-macro/66542 "2024-10-06T06:12:11Z")

</div>

I’m trying to unquote multiple blocks of code dynamically depending on the include option passed when use is called. When I try to map over the handlers, and pass an anonymous function, I’m getting the error that fun doe…

---

## [Constructing qualified function calls in a macro](https://forum.elixirforum.com/t/constructing-qualified-function-calls-in-a-macro/66401)

<div class="topic-metadata">

**Author:** [@spacebat](https://forum.elixirforum.com/u/spacebat)\
**Replies:** 1\
**Last updated:** [September 30, 2024, 10:03pm UTC](https://forum.elixirforum.com/t/constructing-qualified-function-calls-in-a-macro/66401 "2024-09-30T22:03:51Z")

</div>

I’m writing module that will accept a function name and arguments looking like foo(bar, baz) - as Kernel.def/2 does but without a body, and given M is known at compile time I want it to expand to: def foo(bar, baz), do:…

---

## [Macro mental model?](https://forum.elixirforum.com/t/macro-mental-model/61262)

<div class="topic-metadata">

**Author:** [@BradS2S](https://forum.elixirforum.com/u/BradS2S)\
**Replies:** 0\
**Last updated:** [January 27, 2024, 3:26am UTC](https://forum.elixirforum.com/t/macro-mental-model/61262 "2024-01-27T03:26:13Z")

</div>

I watched a great video about a GenServer mental model by Mark Ericksen. I’ve found it very helpful and it has me attempting to make mental models for other aspects of Elixir. Macro(ni) A traveling chef wants to prepar…

---

## [Advice to avoid simple transitive compile-time dependency](https://forum.elixirforum.com/t/advice-to-avoid-simple-transitive-compile-time-dependency/60528)

<div class="topic-metadata">

**Author:** [@gmile](https://forum.elixirforum.com/u/gmile)\
**Replies:** 8\
**Last updated:** [December 23, 2023, 4:31pm UTC](https://forum.elixirforum.com/t/advice-to-avoid-simple-transitive-compile-time-dependency/60528 "2023-12-23T16:31:05Z")

</div>

I am trying to understand why transitive compile-time dependency is there, and what’s a good strategy to remove it. The mix app for example below is available here. In a nutshell, I have these 3 modules: # a.ex defmodu…

---

## [Generate typespec and defstruct from runtime values?](https://forum.elixirforum.com/t/generate-typespec-and-defstruct-from-runtime-values/59498)

<div class="topic-metadata">

**Author:** [@zoedsoupe](https://forum.elixirforum.com/u/zoedsoupe)\
**Replies:** 3\
**Last updated:** [November 5, 2023, 4:23pm UTC](https://forum.elixirforum.com/t/generate-typespec-and-defstruct-from-runtime-values/59498 "2023-11-05T16:23:25Z")

</div>

I’m not very used to macros/compile time manipulation, but I will explain what I want to achieve and would like to know if it’s possible by any way. I’m building a CLI framework for Elixir, with high inspirations on cla…

---

## [@before\_compile with Module.delete\_attribute do not delete @var](https://forum.elixirforum.com/t/before-compile-with-module-delete-attribute-do-not-delete-var/56777)

<div class="topic-metadata">

**Author:** [@shahryarjb](https://forum.elixirforum.com/u/shahryarjb)\
**Replies:** 10\
**Last updated:** [July 1, 2023, 8:28pm UTC](https://forum.elixirforum.com/t/before-compile-with-module-delete-attribute-do-not-delete-var/56777 "2023-07-01T20:28:48Z")

</div>

Hello, Based on typed\_struct/lib/typed\_struct.ex at main · ejpcmac/typed\_struct · GitHub , I am practicing to learn more about macro in elixir, all the things are good but I can not delete the attr I register in module. …

---

## [How do I unquote a sigil parameter to a macro?](https://forum.elixirforum.com/t/how-do-i-unquote-a-sigil-parameter-to-a-macro/56717)

<div class="topic-metadata">

**Author:** [@LukasKnuth](https://forum.elixirforum.com/u/LukasKnuth)\
**Replies:** 2\
**Last updated:** [June 27, 2023, 11:56am UTC](https://forum.elixirforum.com/t/how-do-i-unquote-a-sigil-parameter-to-a-macro/56717 "2023-06-27T11:56:06Z")

</div>

Consider the following simple example: defmodule Test.Foo do defmacro foo(regex) do IO.inspect(regex) end end defmodule Test do require Test.Foo Test.Foo.foo(~r/test/) end {:sigil\_r, \[delimiter: "/", line…

---

## [CodeGen - simple, succint and customizable code generation for your libraries](https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521)

<div class="topic-metadata">

**Author:** [@tmbb](https://forum.elixirforum.com/u/tmbb)\
**Replies:** 17\
**Last updated:** [April 3, 2023, 7:41am UTC](https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521 "2023-04-03T07:41:35Z")

</div>

CodeGen (experimental!) code here: GitHub - tmbb/ex\_code\_gen: Flexible code generation for Elixir · GitHub Suppose you want to have your library generate some code in the user’s module. That is pretty common in Elixir, …

---

## [Injecting code in macro with references to outer scope](https://forum.elixirforum.com/t/injecting-code-in-macro-with-references-to-outer-scope/51131)

<div class="topic-metadata">

**Author:** [@cpiemontese](https://forum.elixirforum.com/u/cpiemontese)\
**Replies:** 0\
**Last updated:** [October 18, 2022, 12:51pm UTC](https://forum.elixirforum.com/t/injecting-code-in-macro-with-references-to-outer-scope/51131 "2022-10-18T12:51:08Z")

</div>

Hello! I’m working with macros and I am trying to inject some code into a use macro to be used “as-is” but I’m having some trouble making it work. I would like to be able to define a \_\_using\_\_ macro which takes some op…

---

## [Get module name as atom within a macro](https://forum.elixirforum.com/t/get-module-name-as-atom-within-a-macro/51069)

<div class="topic-metadata">

**Author:** [@davemccrea](https://forum.elixirforum.com/u/davemccrea)\
**Replies:** 3\
**Last updated:** [October 16, 2022, 12:36pm UTC](https://forum.elixirforum.com/t/get-module-name-as-atom-within-a-macro/51069 "2022-10-16T12:36:51Z")

</div>

Is it possible to generate the module name as an atom within a macro? defmodule MyProject.Schema do defmacro \_\_using\_\_(opts) do quote do use Ecto.Schema import Ecto import Ecto.Changeset im…

---

## [Understanding \_\_using\_\_, with a practical problem](https://forum.elixirforum.com/t/understanding-using-with-a-practical-problem/50425)

<div class="topic-metadata">

**Author:** [@fabioticconi](https://forum.elixirforum.com/u/fabioticconi)\
**Replies:** 1\
**Last updated:** [September 21, 2022, 6:22pm UTC](https://forum.elixirforum.com/t/understanding-using-with-a-practical-problem/50425 "2022-09-21T18:22:36Z")

</div>

Hi all, I’m trying to solve a practical problem - make an ExUnit “data case” with async true as default, instead of false. So if a user does this: use MyTestCase Then async will be true, but if the user specifies use…

---

## [Need help with a 'required' macro](https://forum.elixirforum.com/t/need-help-with-a-required-macro/47809)

<div class="topic-metadata">

**Author:** [@saleyn](https://forum.elixirforum.com/u/saleyn)\
**Replies:** 4\
**Last updated:** [May 19, 2022, 2:27am UTC](https://forum.elixirforum.com/t/need-help-with-a-required-macro/47809 "2022-05-19T02:27:36Z")

</div>

Is there a way to create a macro called required that could be used like this: with required(var) \<- Map.get(map, key) do var else nil -\> raise RuntimeError end I tried something like this, but it doesn’t work: de…

---

## [How to define Macro for a new Type?](https://forum.elixirforum.com/t/how-to-define-macro-for-a-new-type/46852)

<div class="topic-metadata">

**Author:** [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Replies:** 9\
**Last updated:** [April 6, 2022, 1:26pm UTC](https://forum.elixirforum.com/t/how-to-define-macro-for-a-new-type/46852 "2022-04-06T13:26:34Z")

</div>

Background So, I am playing around with a concept named “NewType” and I am taking inspiration from languages like F# and Scala. My objective, for learning purposes mostly, is to build a macro that makes creating this ab…

---

## [Forwarding a match pattern to a custom macro](https://forum.elixirforum.com/t/forwarding-a-match-pattern-to-a-custom-macro/44634)

<div class="topic-metadata">

**Author:** [@ream88](https://forum.elixirforum.com/u/ream88)\
**Replies:** 4\
**Last updated:** [December 19, 2021, 8:50am UTC](https://forum.elixirforum.com/t/forwarding-a-match-pattern-to-a-custom-macro/44634 "2021-12-19T08:50:02Z")

</div>

Hey, I’m currently trying to DRY up some tests which have a lot of boilerplate code in the form of: test "user is updated", %{user\_id: user\_id} do pid = spawn(fn -\> EventStore.subscribe(UserUpdated) assert\_re…

---

## [Quote, defmacro and modules](https://forum.elixirforum.com/t/quote-defmacro-and-modules/44069)

<div class="topic-metadata">

**Author:** [@riccardomanfrin](https://forum.elixirforum.com/u/riccardomanfrin)\
**Replies:** 2\
**Last updated:** [November 24, 2021, 8:06pm UTC](https://forum.elixirforum.com/t/quote-defmacro-and-modules/44069 "2021-11-24T20:06:48Z")

</div>

I’m trying to understand how macros and module definitions interact with each others and I’m having few cases which I don’t understand. The below example has some pseudo doctest to show my doubts. What I need is to supp…

---

## [How to create a macro like def](https://forum.elixirforum.com/t/how-to-create-a-macro-like-def/42189)

<div class="topic-metadata">

**Author:** [@shahryarjb](https://forum.elixirforum.com/u/shahryarjb)\
**Replies:** 1\
**Last updated:** [September 7, 2021, 12:16am UTC](https://forum.elixirforum.com/t/how-to-create-a-macro-like-def/42189 "2021-09-07T00:16:48Z")

</div>

Hello, I try to create a custom macro to implement some functions and structure I need in my project and my goal is to make a custom tools which is created by macro. Please see my macro delete\_list\_item(:roles, DeleteEr…

---

## [Composing macros (using a macro inside another macro)](https://forum.elixirforum.com/t/composing-macros-using-a-macro-inside-another-macro/37127)

<div class="topic-metadata">

**Author:** [@vshesh](https://forum.elixirforum.com/u/vshesh)\
**Replies:** 4\
**Last updated:** [January 29, 2021, 2:58am UTC](https://forum.elixirforum.com/t/composing-macros-using-a-macro-inside-another-macro/37127 "2021-01-29T02:58:29Z")

</div>

I’m trying to use one macro inside another macro and running into issues: defmodule Test do defmacro somemacro(arg1, do: block) do quote do defmodule unquote(arg1) do def unquote(arg1), do: 1 …

---

## [Problem to call function inside macro and improve custom repo crud macro](https://forum.elixirforum.com/t/problem-to-call-function-inside-macro-and-improve-custom-repo-crud-macro/36793)

<div class="topic-metadata">

**Author:** [@shahryarjb](https://forum.elixirforum.com/u/shahryarjb)\
**Replies:** 0\
**Last updated:** [January 12, 2021, 5:36pm UTC](https://forum.elixirforum.com/t/problem-to-call-function-inside-macro-and-improve-custom-repo-crud-macro/36793 "2021-01-12T17:36:27Z")

</div>

Hi, for practicing I wanted to create a custom CRUD function which is using Repo. But on the way I have some problems with basic. Problem 1: in this section I have 2 problems, at first I can’t call my function inside my…

---

## [Passing parameters into defmacros](https://forum.elixirforum.com/t/passing-parameters-into-defmacros/32480)

<div class="topic-metadata">

**Author:** [@lok0613](https://forum.elixirforum.com/u/lok0613)\
**Replies:** 8\
**Last updated:** [June 21, 2020, 9:40pm UTC](https://forum.elixirforum.com/t/passing-parameters-into-defmacros/32480 "2020-06-21T21:40:51Z")

</div>

Hello all, I’m recently doing something similar to ExUnit, keeping a bunch of variables on “setup” block and then pass it to the next block which is “test” just like below. defmodule MyTest do use ExUnit.Case, async:…
