...

/

Generating Less Code by Walking the AST

Generating Less Code by Walking the AST

Learn to maintain the expressive DSL by using much less code.

The trial terminal

Use this terminal to test and execute the commands provided in the lesson:

Terminal 1
Terminal
Loading...

Generating less code while maintaining the DSL

Our Html module is clear and concise, but we had to generate over a hundred macros to make it work. Wouldn’t it be nice to generate less code but still maintain our expressive DSL where all tags can be used as macro calls? Let’s make it happen.

The idea of maintaining the DSL without generating all HTML macros sounds impossible, but step back and remember that Elixir provides full AST access. For example, open up an iex prompt and quote a few arbitrary HTML DSL expressions, and let’s look at the results. Do not load Html module since we are just quoting raw expressions outside the context of our library for this example by executing the following commands:

iex> ast = quote
...
Access this course and 1400+ top-rated courses and projects.