In this presentation, Rich Kilmer looks at creating DSLs in Ruby. Next to the usual distinction between external DSLs (involve writing a grammar and a parser) and internal DSLs (piggyback on the host language's grammar and parser), he explains useful terminology.
Rich distinguishes betwen an implicit and explicit styles - this deals with questions of language design. An example of an explicit DSL is this:
when_shopping do |store|An example for an implicit DSL is this:
store.buy :popcorn
store.buy :soda, @soda_amount
end
when_shopping {Both approaches have benefits and problems. One issue of explicit DSLs is the added token noise, eg. the formal block argument definition. As Rich explains, this noise can look confusing when domain experts are supposed to read and check the code written in the DSL. An implicit DSL looks cleaner - yet it has the problem that it's possible to have scoping problems or that it's unclear where the called functions or methods actually come from. Rich goes into more detail about the trade-offs of either approach and when to use which.
buy :popcorn
buy :soda
}
The talk is rounded off with Rich looking at a very long list of real world DSLs he has written over many years for work. This provides valuable real life experience from designing and working with these DSLs. Lessons include how designing a DSL can help uncover holes in specs, simply because code is less forgiving than prose.
Watch Rich Kilmer's Ruby and the Art of Domain Specific Languages.