# Metaprogramming Elixir (Pragprog)

**URL:** <https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886>\
**Category:** Books\
**Tags:** book-metaprogramming-elixir, pragprog\
**Created:** [June 16, 2017, 11:00pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886 "2017-06-16T23:00:00Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/astonj/32/15_2.png) [@AstonJ](https://forum.elixirforum.com/u/AstonJ)\
**Post date:** [June 16, 2017, 11:00pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/1 "2017-06-16T23:00:00Z")

</div>

![8a84d4edd926bb53dc5e1442a14b23f8608d5eb4_2_1000x1200](https://forum.elixirforum.com/uploads/default/original/3X/7/9/7969902b01ab499961788ddf18e5265d29edc375.jpeg)

> [@](#):
>
> by Chris McCord
> 
> Write code that writes code with Elixir macros. Macros make metaprogramming possible and define the language itself. In this book, you’ll learn how to use macros to extend the language with fast, maintainable code and share functionality in ways you never thought possible. You’ll discover how to extend Elixir with your own first-class features, optimize performance, and create domain-specific languages.
> 
> * * *
> 
> Metaprogramming is one of Elixir’s greatest features. Maybe you’ve played with the basics or written a few macros. Now you want to take it to the next level. This book is a guided series of metaprogramming tutorials that take you step by step to metaprogramming mastery. You’ll extend Elixir with powerful features and write faster, more maintainable programs in ways unmatched by other languages.
> 
> You’ll start with the basics of Elixir’s metaprogramming system and find out how macros interact with Elixir’s abstract format. Then you’ll extend Elixir with your own first-class features, write a testing framework, and discover how Elixir treats source code as building blocks, rather than rote lines of instructions. You’ll continue your journey by using advanced code generation to create essential libraries in strikingly few lines of code. Finally, you’ll create domain-specific languages and learn when and where to apply your skills effectively.
> 
> When you’re done, you will have mastered metaprogramming, gained insights into Elixir’s internals, and have the confidence to leverage macros to their full potential in your own projects.

_Don’t forget you can get 35% off the ebook using the code ‘[devtalk.com](http://devtalk.com)’ :023:_

> **[Search](https://pragprog.com/search/?q=m)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

---

<div class="post-metadata">

**Author:** ![leifericf](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/leifericf/32/39367_2.png) [@leifericf](https://forum.elixirforum.com/u/leifericf)\
**Post date:** [August 2, 2018, 6:59pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/2 "2018-08-02T18:59:56Z")

</div>

I’ve already bought this book a few weeks ago, along with most of the other Elixir-related books at Pragprog. I think it’s a bit too advanced for me right now, but I’m looking forward to reading it after I’ve gotten through Programming Elixir and Programming Phoenix.

---

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/astonj/32/15_2.png) [@AstonJ](https://forum.elixirforum.com/u/AstonJ)\
**Post date:** [August 2, 2018, 10:43pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/3 "2018-08-02T22:43:42Z")

</div>

Let us know how you get on Leif! I also have the book, but haven’t got around to reading it yet :icon_redface:

Anybody read it? Thoughts? 🙂

---

<div class="post-metadata">

**Author:** ![blatyo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/blatyo/32/4554_2.png) [@blatyo](https://forum.elixirforum.com/u/blatyo)\
**Post date:** [August 3, 2018, 2:05am UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/4 "2018-08-03T02:05:09Z")

</div>

I’ve read it and I liked it. It covers generating code from an external file, which is about 80% of the macro code I write. The section on generating an HTML DSL, is probably the least useful for me. I believe at some point it started using an actor to track changes, which I didn’t really like. The Phoenix router DSL is pretty interesting, but the patterns used there weren’t in the book. So, I recommend diving into the Phoenix, Plug, and Ecto code after reading the book. I’d say the book gave me the building blocks to read and understand those. Reading those, personally gave me a better idea of what’s possible.

---

<div class="post-metadata">

**Author:** ![pillaiindu](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@pillaiindu](https://forum.elixirforum.com/u/pillaiindu)\
**Post date:** [August 10, 2018, 2:33pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/5 "2018-08-10T14:33:11Z")

</div>

@chrismccord, are there any plans for 2nd edition of this book?

---

<div class="post-metadata">

**Author:** ![chrismccord](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chrismccord/32/24233_2.png) [@chrismccord](https://forum.elixirforum.com/u/chrismccord)\
**Post date:** [August 10, 2018, 4:08pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/6 "2018-08-10T16:08:46Z")

</div>

The book is still 100% relevant and there’s not yet a need for a 2nd edition as Elixir’s AST and metaprogramming features are fortunately quite stable. I think the only thing that needs updated in the book today is the `break` feature of the `while` macro will warn on null arity parens 🙂

---

<div class="post-metadata">

**Author:** ![frigidcode](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/frigidcode/32/7594_2.png) [@frigidcode](https://forum.elixirforum.com/u/frigidcode)\
**Post date:** [August 10, 2018, 6:55pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/7 "2018-08-10T18:55:11Z")

</div>

> [@AstonJ](#):
>
> _Don’t forget you can get 35% off with your forum discount, use code ‘ElixirForum’ :023:_
> 
> [https://pragprog.com/book/cmelixir/metaprogramming-elixir](https://pragprog.com/book/cmelixir/metaprogramming-elixir)

$7.15 for the ebook, that’s a steal!

---

<div class="post-metadata">

**Author:** ![sfusato](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sfusato/32/11569_2.png) [@sfusato](https://forum.elixirforum.com/u/sfusato)\
**Post date:** [November 15, 2018, 5:53pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/8 "2018-11-15T17:53:50Z")

</div>

I have just followed the book with no stumbling blocks along the way. I honestly expected to face some issues considering when the book was released, but other than some deprecation warnings which were expected, everything else was smooth sailing.

Solid book with very practical examples. Thank you Chris!

---

<div class="post-metadata">

**Author:** ![Ted](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ted/32/15126_2.png) [@Ted](https://forum.elixirforum.com/u/Ted)\
**Post date:** [August 11, 2019, 9:29pm UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/9 "2019-08-11T21:29:21Z")

</div>

I picked up _Metaprogramming Elixir_ during PragProg’s recent Christmas in July sale; the book is already modestly priced but was a steal with the discount.

I’m fairly new to Elixir, so my original intent was to read the book once I had a bit more exposure and experience under my belt.

But then I thought, “Well, I already have a rudimentary familiarity with macros from dabbling in Clojure (a lisp). I’ll be learning Ecto and Phoenix soon and I know there are some DSLs and `use` going on there, so it’d be really nice to have some intuition about what’s happening under the hood. Let’s just take a little peek at the first chapter and see.”

I was hooked.

Chris McCord’s clear writing style and obvious focus on reader ergonomics, e.g., repeating relevant bits of code, makes the book very readable and engaging.

Regarding content, there were plenty of moments where I thought, “wait, we’re going to do _what_ next?! Okay, I’ve got time to read a few more pages…”

And, as I had hoped, the book covers DSLs and `use`, but it also covered so much more.

I was introduced to Elixir’s AST and the concept of macro hygiene, along with repeatable recipes for building DSLs and testing macros.

As an Elixir noob, I don’t think I’ll be writing my own for-real macros just yet, but I feel that I’ve gain a lot of valuable intuition about how the language works, which was my goal.

This is definitely a resource I’ll revisit once I get more experience.

Thank you @chrismccord!

Other notes:

I read the book cover-to-cover in about three days, mostly during lunch breaks and at bedtime.

I recommend reading the book start-to-finish.

I found the book to be e-reader friendly (Kindle Paperwhite, landscape).

---

<div class="post-metadata">

**Author:** ![AstonJ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/astonj/32/15_2.png) [@AstonJ](https://forum.elixirforum.com/u/AstonJ)\
**Post date:** [August 12, 2019, 12:31am UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/10 "2019-08-12T00:31:44Z")

</div>

> [@Ted](#):
>
> Chris McCord’s clear writing style and obvious focus on reader ergonomics, e.g., repeating relevant bits of code, makes the book very readable and engaging.

> [@Ted](#):
>
> I found the book to be e-reader friendly (Kindle Paperwhite, landscape).

That’s good to know! Bought the book ages ago, just haven’t got round to it yet..

---

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fl4m3ph03n1x/32/11709_2.png) [@Fl4m3Ph03n1x](https://forum.elixirforum.com/u/Fl4m3Ph03n1x)\
**Post date:** [April 1, 2022, 8:28am UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/11 "2022-04-01T08:28:45Z")

</div>

Hello all, recently this book came into my attention, namely I am trying to do a macro that creates a module dynamically. Let’s say, something like this:

```elixir
defmodule Name do
  @opaque t :: {:name, String.t()}

  @spec new(val :: String.t()) :: t
  def new(val) when is_binary(val), do: {:name, val}

  @spec extract(name :: t) :: String.t()
  def extract({:name, val}), do: val

  @spec name?(data :: any) :: boolean()
  def name?({:name, val}) when is_binary(val), do: true
  def name?(_data), do: false

  @spec is_name(data :: any) ::
          {: __block__ | {:., [], [:andalso | :erlang, ...]}, [],
           [{:= | {any, any, any}, [], [...]}, ...]}
  defguard is_name(value)
           when is_tuple(value) and elem(value, 0) == :name and is_binary(elem(value, 1))
end

```

which would be generated by invoking:

```elixir
deftype Name, String.t() 

```

Now I have checked the mini-video for the book:

> **[Metaprogramming Elixir](https://pragprog.com/titles/cmelixir/metaprogramming-elixir/)**
>
> Use macros to extend the Elixir language with fast, maintainable code, and share functionality in ways you never thought possible.

And as an intro, it covers a lot. But after checking the index I have some questions:

- does the book cover my specific use case? (does it teach how to dynamically generate modules?)
- does the book cover how to include typespecs in the generated code?
- How many pages does the book have?

---

<div class="post-metadata">

**Author:** ![Aetherus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aetherus/32/17203_2.png) [@Aetherus](https://forum.elixirforum.com/u/Aetherus)\
**Post date:** [April 1, 2022, 9:31am UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/12 "2022-04-01T09:31:01Z")

</div>

> [@Fl4m3Ph03n1x](#):
>
> does the book cover my specific use case? (does it teach how to dynamically generate modules?)

Yes and no. With the techniques in this book, you’ll know how macros work. How to implement your desired syntax is up to you.

> [@Fl4m3Ph03n1x](#):
>
> does the book cover how to include typespecs in the generated code?

No, but typespecs are just “code”, which turn into abstract syntax trees anyway, so there’s no difference between “normal” Elixir code and typespecs.

> [@Fl4m3Ph03n1x](#):
>
> How many pages does the book have?

Around 130 pages. I personally feel this book is not thick enough, so don’t expect in-depth explanations. In order to write your own macros, you’ll experience a whole lot of try and errors.

---

<div class="post-metadata">

**Author:** ![BartOtten](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bartotten/32/25440_2.png) [@BartOtten](https://forum.elixirforum.com/u/BartOtten)\
**Post date:** [April 1, 2022, 10:17am UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/13 "2022-04-01T10:17:45Z")

</div>

Read the book and can confirm the praises. Currently working on my first lib which is macro heavy. Chris’ book took the magic out of macro’s, they are now ‘just plain Elixir’ to me.

---

<div class="post-metadata">

**Author:** ![Aetherus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/aetherus/32/17203_2.png) [@Aetherus](https://forum.elixirforum.com/u/Aetherus)\
**Post date:** [April 1, 2022, 11:04am UTC](https://forum.elixirforum.com/t/metaprogramming-elixir-pragprog/5886/14 "2022-04-01T11:04:58Z")

</div>

Yeah, I finished reading it, too. When I was trying to build an ecto\_query-ish library, I also learned a lot beyond that book.
