Metaprogramming Elixir
Write Less Code, Get More Done (and Have Fun!)
by Chris McCord
Version: P1.0 (February 2015)
Copyright 2015 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
The team that produced this book includes:
Jacquelyn Carter (editor)
Cathleen Small (copyeditor)
Dave Thomas (typesetter)
Janet Furlow (producer)
Ellie Callahan (support)
For international rights, please contact .
For the Best Reading Experience...
We strongly recommend that you read this book with the publisher defaults setting enabled for your reading device or application. Certain formats and characters may not display correctly without this setting. Please refer to the instructions for your reader on how to enable the publisher defaults setting.
To my lovely wife, Jaclyn.
Table of Contents
Copyright 2015, The Pragmatic Bookshelf.
Early praise for Metaprogramming Elixir
This book is exactly what the young Elixir community needs! Chris McCord does an elegant job of laying out Elixir metaprogramming step by step, with practical and wonderfully instructive examples throughout.
Bruce Tate |
President, RapidRed, LLC |
Whether youre new to Elixir or a seasoned pro, this compact book will give you the foundation you need to harness the full power of Elixir. A joy to read as it gently walks the reader toward metaprogramming mastery, its a thoughtful and practical guide to metaprogramming youll want to visit again and again.
Matt Sears |
CEO Littlelines |
Chris is the person to be writing this book; reading his work in open source is how I learned how to use macros. This book filled in the gaps of my understanding and improved my intuition for how Elixir the language works.
Jason Stiebs |
Partner, RokkinCat |
Metaprogramming Elixir made me want to run out and write code that writes code for me! Great voice and compelling examples!
Chris has a habit of seeing past the surface of a technology. In Metaprogramming Elixir, Chris demystifies the foundation of Elixir itself, opening the door for every Elixir programmer to build applications in fun, powerful ways.
Ryan Cromwell |
Technical director, Sparkbox |
A treasure trove of metaprogramming patterns, this book is just whatthe community needs to communicate the power, extensibility, andpracticality of metaprogramming in Elixir. After reading it, youllknow how and why to use metaprogramming both responsibly andirresponsibly. Definitely a must-have for anyone wanting to go beyondthe basics of a beautiful language.
Gabriel Jaldon |
Web developer, open source enthusiast |
Acknowledgements
This book wouldnt have been possible without the help of a number of people whose hard work and support cant go without mention. These names deserve recognition for making the book what it is today.
Jos ValimCreator of Elixir:
Metaprogramming Elixir clearly wouldnt have been possible without all the hard work that Jos dedicated to crafting this wonderful language. Beyond that, though, he has been a true community leader and a helpful friend. His welcoming and kind nature has shaped Elixirs community and set a precedent for how an OSS project should be run. He has been constant source of help and inspiration along the way.
Matt SearsFounder, Littlelines:
As my boss at my full-time job, Matt gave me the flexibility to make this book possible. From a work atmosphere that fosters personal growth, to his support for community outreach, he helped make this book a reality. For that, Im truly thankful.
Jacquelyn CarterEditor:
Working closely with Jackie on this book has been a great pleasure. Jackies guiding feedback and encouragement led to a work that Im extremely proud of. This book would not have been the same without her.
The Publishers:
It has been an honor to work with the Pragmatic Programmers. Dave Thomas and Andy Hunt welcomed me into the Prag family, and their close-knit operation made me feel right at home. Susannah Pfalzer offered wisdom and encouragement while pushing me to do my best. Im extremely grateful for the entire Prag team and the level of care I received throughout this journey.
The Reviewers:
The reviewers have my gratitude for dedicating their time and knowledge to help make this book the best it could be. Jos Valim, Bruce Tate, Jason Stiebs, Matt Sears, Zander Hill, Ryan Cromwell, and Gabriel Jaldon all took time out of their busy lives to offer guidance and support. Jason Stiebs deserves particular credit for being available at all hours of the day and night to offer feedback throughout the process.
Copyright 2015, The Pragmatic Bookshelf.
Introduction
In 2012, Jos Valim announced that he was working on his own programming language. At the time, I thought he was crazy. Little did I know the impact his creation would have on the way I view programming. Since discovering it, I cant bear to put down the keyboard. The amount of care that went into the languages design and the level of power it gives you is truly freeing. Its the only language Ive found that makes it a joy to build scalable, fault-tolerant systems. Combined with all the innovations from the Erlang ecosystem, its a language you can easily fall in love with. Throughout this book, youll find out why.
Were going to explore metaprogramming in Elixir from the ground up. Along the way, youll level up your programming skills and discover the full potential of Elixirs macro system. After youre finished, youll know the ins and outs of Elixir at a fundamental level, and youll be able to write incredible libraries that let you do more with less code.
MetaprogrammingAn Essential Feature
Metaprogramming is a must on my list of language requirements. It puts you in control to extend the language to suit your projects needs and lets you write powerful libraries that would be otherwise impossible. I have a strong web development background, and when I started with Elixir, I immediately realized how well suited it would be for a web framework. Phoenix was born out of the realization that I could have the best of both words in a web framework: a language that makes it a joy to write applications, with a runtime that is fast and scalable. With Elixir, Phoenix doesnt have to choose between productivity and scalability. Metaprogramming gives us both.