Best resource to learn DSL?

I want to learn DSL. Don’t know how to write one. What;s the best introductory resource?

I see some macro being used here.

Is DSL only about using Macros to help write other functions easily?

1 Like

More or less, yep. You can’t really learn DSL per se, DSLs are custom languages that you build yourself. For that you need to know Elixir macros.

I recently started the Metaprogramming Elixir book and I enjoy what I saw and tried so far, so that’s one possible resource for you.

2 Likes

DSL is a concept closely associated with Domain Driven Design, the idea is to have a way to easily express things inside a particular domain. For the technical part, you could learn (like dimitarvp said) Metaprogramming Elixir / Elixir Macro. For the design part, you could learn Domain Driven Design (i would recommend Implementing Domain Driven Design for more easier time of reading DDD).

1 Like

DSL are used in runtime and compile time. At runtime they help customising the software through scripts or rules. Systems expose ability to customise the functionality/behaviour through the DSLs. For example rules editor in a firebase - Lenguaje de las reglas de seguridad  |  Firebase Documentation . Quite a lot of google cloud services use Common Expression Language - GitHub - google/cel-spec: Common Expression Language -- specification and binary representation

The complete guide to (external) Domain Specific Languages - Strumenta covers a lot of things about DSLs.

Elixir has metaprogramming through macros which is compile time.Ecto has DSL for simplifying queries for accessing the data. If your use case is writing a compile time DSL - elixir metaprogramming makes it easy. Runtime DSL in elixir needs a lot of things - like interpreter, etc to be implemented.

Implementing a DSL needs grammar to be formalised as language spec.

What are you trying to achieve with DSL in your application? Is it runtime or compile time ?

3 Likes

That book is generally highly recommended, but if you want something shorter to start off with there’s also Saša Jurić’s blog series starting here . I haven’t gone through it yet, but I’ve found Jurić’s other writings lucid.

3 Likes

Thank you such detailed reply.

My use case only involves writing some macros to help write other code easier at compile time.

Thank you. Like @crispinb pointed out Sasa Juric’s blog post series is a good place to start.

Metaprogramming Elixir -Metaprogramming Elixir: Write Less Code, Get More Done (and Have Fun!) by Chris McCord is a book which is completely about Metaprogramming. You will find a coupon code in this forum for most of the elixir books.

One thing about metaprogramming is - it may not be needed most of the time. Using import and functions might solve the problem. Chapter 6 of Metaprogramming Elixir book explains more about it.

I learnt a lot by searching about how elixir, ecto and phoenix use defmacros in their code base:

2 Likes