8

Metaprogramming – Code That Writes Code

In this chapter, we will learn about one of the most powerful features of Elixir, metaprogramming. Writing a simple piece of Elixir code is referred to as programming, and writing Elixir code that programmatically injects behavior into other Elixir code is referred to as metaprogramming. Metaprogramming allows us to extend Elixir’s features and facilitate developer productivity by automating the otherwise tedious and manual process of adding repetitive boilerplate code.

Let’s take the Phoenix router, for example:

blog/lib/blog_web/router.ex

 defmodule BlogWeb.Router do   # ..   pipeline :browser do     plug :fetch_session     plug :accepts, ["html"]   end   scope "/", BlogWeb do     pipe_through [:browser] ...

Get Build Your Own Web Framework in Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.