Domain Specific Languages

Domain Specific Languages

Foreword

Domain Specific Languages (DSL) allow developers to tailor their application to a particular domain. You don’t need macros in order to have a DSL: every data structure and every function you define in your module is part of your Domain Specific Language.

For example, imagine we want to implement a Validator module which provides a data validation domain specific language. We could implement it using data structures, functions or macros. Let’s see what those different DSLs would look like:

# 1. data structures
import Validator
validate user, name: [length: 1..100],
               email: [matches: ~r/@/]

# 2. functions
import Validator
user
|> validate_length(:name, 1..100)
|> validate_matches(:email, ~r/@/)

# 3. ma