Please disable your adblock and script blockers to view this page

Metaprogramming in Elixir


Elixir
OG
LISP
macro!In
HelloWeb


Metaprogramming
Chris McCord
Chris McCord’s
Saša Jurić
Don’t
Jesse Anderson

No matching tags


AST

No matching tags


Phoenix
sum(a

No matching tags

Positivity     45.00%   
   Negativity   55.00%
The New York Times
SOURCE: https://serokell.io/blog/elixir-metaprogramming
Write a review: Hacker News
Summary

During the compilation process, every computer program is transformed into an abstract syntax tree (AST) – a tree structure that enables the computer to understand the contents of the program.In Elixir, each node of the AST (except basic values) is a tuple of three parts: function name, metadata, function arguments.Elixir enables us to access this internal AST representation via quote.We can modify these ASTs with macros, which are functions from AST to AST that are executed at compile-time.You can use macros to generate boilerplate code, create new language features, or even build domain-specific languages (DSLs).Actually, a lot of the language constructs that we regularly use in Elixir such as def, defmodule, if, and others are macros. But it also gives us access to a magical use HelloWeb, :router to have everything ready for quick webdev action whenever we need.Now, look below use.Yup, more macros.pipeline and plug define pipelines of plugs, which are functions of functions that transform the connection data structure.While the previous macro was used for convenient one-line imports, this one helps to write pipelines in a very clear and natural language.And, of course, the routing table is a macro as well.scope, pipe_through, get – all macros.In fact, the whole module is macros and an if statement (which is a macro) that adds an import statement and executes a macro.I hope that this helps you see how metaprogramming is at the heart of Elixir.Now, let’s try to build our own macro.Elixir and currying don’t really vibe together.

As said here by